我近段的一個應用中需要消息驅(qū)動POJO。 最近也聽說Spring 2.0開始支持消息驅(qū)動POJO了,但我的應用使用的Spring1.2。 從網(wǎng)上找了一些文章看來都比較舊了,并且近段ActiveMQ處于Apache的孵化器中,包的結構也有些改變,原來的ActiveMQ中的JCA容器部分也從ActiveMQ中獨立也來成了Jencks項目。 后來參考這些文章和Jencks官網(wǎng)上的說明成功實現(xiàn)了消息驅(qū)動POJO。 下面記錄了我配置的過程: - 搞定相依賴的包。
a.Spring相關的一些包,這部分就不再啰嗦了。 b.ActiveMQ相關的包 activemq-ra-4.01 concurrent-1.3.4 incubator-activemq-4.0.1 c.JCA容器 jencks-1.3-all - 服務端配置
代碼 - <bean id="helloBean" class="mdp.demo.HelloBean" />
-
- <bean id="jmsTransactionManager"
- class="org.jencks.factory.TransactionManagerFactoryBean" />
-
- <bean id="activeMQContainer" class="org.jencks.JCAContainer">
- <property name="bootstrapContext" >
- <bean id="bootstrapContext" class="org.jencks.factory.BootstrapContextFactoryBean">
- <property name="transactionManager" ref="jmsTransactionManager"/>
- </bean>
- </property>
-
- <property name="resourceAdapter">
- <bean id="activeMQResourceAdapter"
- class="org.apache.activemq.ra.ActiveMQResourceAdapter">
- <property name="serverUrl"
- value="tcp://localhost:61616" />
- </bean>
- </property>
- </bean>
-
- <bean id="HelloMDP" class="org.jencks.JCAConnector">
- <property name="jcaContainer" ref="activeMQContainer" />
- <property name="transactionManager" ref="jmsTransactionManager" />
- <property name="activationSpec">
- <bean
- class="org.apache.activemq.ra.ActiveMQActivationSpec">
- <property name="destination" value="Hello.Queue" />
- <property name="destinationType"
- value="javax.jms.Queue" />
- </bean>
- </property>
- <property name="ref" value="helloBean" />
- </bean>
- 開發(fā)HelloBean
代碼 - package mdp.demo;
-
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageListener;
-
- public class HelloBean implements MessageListener {
-
- public void onMessage(Message msg) {
- try {
- String name = msg.getStringProperty("name");
- if (name == null) {
- name = "World";
- }
-
- System.out.println("Hello " + name + "!");
- } catch (JMSException e) {
- e.printStackTrace();
- }
-
- }
-
- }
-
- 客戶端配置,這里直接使用Spring的JmsTemplate
代碼 - <bean id="jmsTemplate"
- class="org.springframework.jms.core.JmsTemplate">
- <property name="defaultDestinationName" value="Hello.Queue" />
- <property name="connectionFactory" ref="connectionFactory" />
- </bean>
- <bean id="connectionFactory"
- class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL" value="tcp://localhost:61616" />
- </bean>
- 客戶端示例程序
代碼 - package mdp.demo;
-
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import javax.jms.Message;
- import javax.jms.Session;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
-
- public class MDPClient {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "spring-client.xml");
-
- JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
- template.setDefaultDestinationName("Hello.Queue");
- template.send(new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- MapMessage message = session.createMapMessage();
- message.setStringProperty("name", "消息驅(qū)動POJO");
- return message;
- }
- });
-
-
- }
- }
上面的示例代碼實現(xiàn)了消息驅(qū)動POJO,但是POJO類仍需實現(xiàn)MessageListener接口。 上面的配置也不支持XA事務,Jencks官網(wǎng)上有說明,但我沒有配置成功(Hibernate和JMS)。有哪位兄弟配置成功請告知,謝謝。
|