Monday, February 8, 2010

Sending and Receiving Message using Spring, ActiveMQ and JMS

Project: Spring_ActiveMQ
IDE: Eclipse
JARS:
activemq-core-5.3.0.jar
commons-lang-2.1.jar
commons-logging-1.1.1.jar
concurrent-1.3.4.jar
jms-1.1.jar
log4j-1.2.9.jar
spring-2.0-rc3.jar

XML Configuration
Shared Context:

<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="requestQ"/> </bean> <bean id="replyQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="replyQ"/> </bean> <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean>


Client Context:

<bean id="replyNotifier" class="com.somellc.spring_activemq.ReplyNotifier"/> <bean id="listener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <property name="delegate" ref="replyNotifier"/> <property name="defaultListenerMethod" value="notify"/> </bean> <bean id="container" class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageListener" ref="listener"/> <property name="destination" ref="replyQ"/> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultDestination" ref="requestQ"/> </bean>


Server Context:

<bean id="registrationService" class="com.somellc.spring_activemq.RegistrationService"/> <bean id="listener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <property name="delegate" ref="registrationService"/> <property name="defaultListenerMethod" value="processRequest"/> <property name="defaultResponseDestination" ref="replyQ"/> </bean> <bean id="container" class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageListener" ref="listener"/> <property name="destination" ref="requestQ"/> </bean>


Client.java
ApplicationContext context = new ClassPathXmlApplicationContext(filename);
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
jmsTemplate.convertAndSend(request);

Server.java
new ClassPathXmlApplicationContext(filename);
System.in.read();

ReplyNotifier.java
Has method notify()

No comments:

Post a Comment