在現有的jstorm框架下,有一個需求:jstorm要對接mysql數據庫的實時讀取數據, 通過bolt處理,可能要調用service層的框架,最后保存到數據庫。
在網上尋找了一下,發現storm集成spring的資料非常少,有的也只是簡單描述,現把搭建過程的一些問題和注意事項詳細列出。
1、pom文件、jstorm+spring+mybatis? 網上大把的資料,不在這里詳細累述。需要注意就是版本兼容問題,可以到www.mvnreposity.com去查看一下
2、jstorm容器集成spring容器,不能采用@autowire 注入的方式,只能在component(spout/bolt)中獲取bean,可以寫一個公共類,方便以后相同操作。


public class SpringContext implements ApplicationContextAware{private static ClassPathXmlApplicationContext applicationContext ;public static synchronized void SpringContextInit(){if (applicationContext==null){applicationContext =new ClassPathXmlApplicationContext(new String[]{"application.xml"}); // applicationContext.start(); }}public static <T> T getBean(String name,Class<T> clazz){if (applicationContext==null){SpringContext.SpringContextInit();}return applicationContext.getBean(name,clazz);}@Overridepublic void setApplicationContext(ApplicationContext arg0) throws BeansException {applicationContext=(ClassPathXmlApplicationContext) arg0;} }
3、序列化問題:通過本地測試? 加不加序列化都無所謂? ?都能跑通, 建議加上,因為現在還沒有上線集群測試
4、將獲取bean的操作 放在open和prepare里、在spout中獲取數據源的方法放在open中,nextTuple只負責提交數據到stream中、在bolt中處理數據的方法放在excute中。
?