Apache Storm介紹

http://www.cnblogs.com/Jack47/p/storm_intro-1.html

http://ifeve.com/getting-started-with-storm-1/
http://ifeve.com/getting-started-with-storm-2/
http://ifeve.com/getting-started-with-storm-3/
http://ifeve.com/getting-started-with-storm-4/
http://ifeve.com/getting-started-with-storm-5/
http://ifeve.com/getting-started-with-storm6/
http://ifeve.com/getting-started-with-storm7/
http://ifeve.com/getting-started-of-storm8/

http://ifeve.com/author/onenote/

廣告

Spring Propagation types

Spring交易的屬性介紹

https://openhome.cc/Gossip/SpringGossip/TransactionAttribute.html

Propagation behavior:

MANDATORY
Support a current transaction, throw an exception if none exists.

Does not start a new Transaction, just checks whether a transaction is active (must be inside either another @Transactional method call or a programmatically created transaction)

NESTED
Execute within a nested transaction if a current transaction exists, behave likePROPAGATION_REQUIRED else.

Start a nested transaction if a transaction exists, start a new transaction otherwise.

NEVER
Execute non-transactionally, throw an exception if a transaction exists.

Does not start a transaction. Fails if a transaction is present.

NOT_SUPPORTED
Execute non-transactionally, suspend the current transaction if one exists.

Does not start a transaction. Suspends any existing transaction.

REQUIRED
Support a current transaction, create a new one if none exists.

If a transaction exists, use that, if not, create a new one. In 95% of cases, this is what you need.

REQUIRES_NEW
Create a new transaction, suspend the current transaction if one exists.

Always creates a new transaction, no matter if an existing transaction is present. If there is, it will be suspended for the duration of this method execution.

SUPPORTS
Support a current transaction, execute non-transactionally if none exists.

Can use a transaction if one is present, but doesn’t need one (and won’t start a new one either)

Java Hibernate.initialize() issue

最近同事遇到備份table(Model)某些幾筆資料會處於lazyinitializer狀態,欄位全是null的情形,
這幾筆正好都是在之前有get過其他Model然後該Model有@ManyToOne關連到這個table的,
由於是直接用dao.list()撈出來,dao裡面是單純的用session.createCriteria(clazz).list()做,
奇怪的是即使已經做了Hibernate.initialize(XXX),用debug看撈出來的XXX屬性還是null,
正當百思不得其解時,我用debug看getter XXX方法,結果竟然有值!
原來Hibernate.initialize()是對proxy做initialze,不是對該Model直接賦予值,因此還是要從getter拿屬性才會是正確的,
然後再看同事的備份table方法,是撈出來的Model List直接透過new ObjectMapper().writeValueAsString(list)轉成json,
這個list並沒有經過getter拿屬性,因此才會有幾筆lazyinitializer的資料欄位是null,
其實fetch = FetchType.LAZY屬性Hibernate session會從proxyByKey的Hashmap拿,這種必須透過getter才能真正拿到值,
(只要宣告成fetch = FetchType.LAZY的屬性都有機會變null,@OneToMany的屬性也是一樣,只是@ManyToOne是每次必變null)

所以備份table issue總結如下:
解法1:
不要偷懶全部屬性都用getter,但這樣每個屬性都要寫…

解法2:
還是維持new ObjectMapper().writeValueAsString(list)
做dao.list()之前先session.clear() –> 強迫session中斷就可以更新model,注意flush()或refresh()也沒用,一定要用clear()

解法3:
把XXX屬性的@ManyToOne(fetch = FetchType.LAZY)改成FetchType.EAGER
這樣每次撈此Model都會順便直接初始化XXX屬性

JVM分析的2個工具(JVisualVM&GCViewer)

看OutOfMemory:
a.加jvm參數:
-XX:+HeapDumpOnOutOfMemoryError
b.手動產生dump file: (模擬OutOfMemory發生, OOM產生檔案路徑: %TOMCAT_HOME%/bin/java_pid.hprof)
用jdk/bin下的jconsole打開pid
到 MBeans > com.sun.management > HotSpotDiagnostics > Operations |DumpHeap > p1輸入要產生的hprof檔名(預設是String, 檔案產生在jconsole同一層目錄)
c.用jdk/bin下的jvisualvm打開java_pid.hprof檔案

看GC log:
a.加jvm參數:
-verbose:gc -Xloggc:gc.log
b.執行tomcat (產生檔案路徑: %TOMCAT_HOME%/bin/gc.log)
c.下載GCViewer.jar用此工具打開gc.log

參考資料: JVM分析的2個工具
http://www.blogjava.net/fastzch/archive/2008/07/20/216240.html

Authentication using certificates, Tomcat and Spring security

Use LDAPS for Spring Security:
http://l-lin.github.io/2014/09/09/Auth_with_certificates_Tomcat_spring/

– 顯示LDAPS certificate:
openssl s_client -showcerts -connect myserver:636
– 存成X509 certificate檔:
echo -n | openssl s_client -connect myserver:636 | sed -ne ‘/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p’ > myserver.crt

– 儲存certificate到JRE: (預設密碼:changeit)
keytool -import -keystore /opt/jdk7/jre/lib/security/cacerts -alias myserver -file myserver.crt
– 刪除certificate:
keytool -delete -alias myserver -keystore /opt/jdk7/jre/lib/security/cacerts -storepass changeit

– Client certificate over SSL
http://stackoverflow.com/questions/875467/java-client-certificates-over-https-ssl

-Djavax.net.ssl.keyStoreType=pkcs12
-Djavax.net.ssl.trustStoreType=jks
-Djavax.net.ssl.keyStore=clientcertificate.p12
-Djavax.net.ssl.trustStore=gridserver.keystore
-Djavax.net.debug=ssl # very verbose debug
-Djavax.net.ssl.keyStorePassword=$PASS
-Djavax.net.ssl.trustStorePassword=$PASS

Java VM Options You Should Always Use in Production

實用參數 備份一下

http://blog.sokolenko.me/2014/11/javavm-options-production.html

java.util.Observer 與 java.util.EventListener 的比較

A. Observer 在 JDK 1.0 加入,比較單純,角色只有觀察者與被觀察者
B. EventListener 在 JDK 1.1 加入,比較複雜,角色有監聽者、事件與事件來源

417812_1304583414UuPo

417812_1304586849gPUL

觀察者 (監聽者):
A. interface Observer – 有 update(Observable o, Object arg) 方法
B. interface EventListener – 沒有宣告方法,通常會自定一個 subinterface 帶有 handleEvent() 方法

被觀察者 (事件):
A. class Observable – 有實做 addObserver(Observer o) 與 notifyObservers() 方法,當被觀察者要發送訊息時需呼叫 notifyObservers()
B. class EventObject – 有一個 EventSource 成員,它是產生這個 Event 的來源,addListener(EventListener l) 與 notifyListeners() 是定義在 EventSource 物件上。例如:ClickEvent 是來自 Button,Button 才有 addClickListener() 與 click() 方法,ClickEvent 僅作為傳遞消息的中介 (mediator)。

程式範例:
A. Observer:(略)

B. EventListener:
JButton button1 = new JButton();
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JButton jButton = (JButton) actionEvent.getSource();
int r = (int) (Math.random() * 100);
int g = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
jButton.setBackground(new Color(r, g, b));
}
};
button1.addActionListener(actionListener);
button1.doClick();

參考文章:JAVA设计模式之Observer模式