Spring3 and MDB Integration(I)Producer and Consumer
1. configure JMS in JBOSS
JBOSS_HOME/server/default/deploy/messaging/desinations-service.xml
add some content:
<!-- Register User Send/Receive Queue -->
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=IUserQueue">
<attribute name="JNDIName">queue/IUserQueue</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<!-- Register User Send/Receive Topic -->
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=IUserTopic">
<attribute name="JNDIName">topic/IUserTopic</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
2. Spring configuration file jms-easymdbproxy-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>XAConnectionFactory</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="destination" />
<!--
true for Topic
false for Queue
-->
<property name="pubSubDomain">
<value>false</value>
</property>
<!-- message waiting time(ms) -->
<property name="receiveTimeout">
<value>30000</value>
</property>
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<!--
<property name="jndiName">
<value>topic/IUserTopic</value>
</property>
<property name="jndiName">
<value>queue/IUserQueue</value>
</property>
-->
<property name="jndiName">
<value>queue/IUserQueue</value>
</property>
</bean>
<!-- message producer -->
<bean id="messageProducer" class="com.sillycat.easymdbproxy.mdb.MessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- message receiver -->
<bean id="messageConsumer" class="com.sillycat.easymdbproxy.mdb.MessageConsumer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
</beans>
3. Java class that implement the function
MessageProducer.java:
package com.sillycat.easymdbproxy.mdb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.sillycat.core.webservice.model.IUser;
public class MessageProducer
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final IUser iUser)
{
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
message.setObject(iUser);
System.out.println("send message success!!");
return message;
}
});
}
}
Java class that consumer the JMS, MessageConsumer.java:
package com.sillycat.easymdbproxy.mdb;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import org.springframework.jms.core.JmsTemplate;
import com.sillycat.core.webservice.model.IUser;
public class MessageConsumer
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public IUser receiveMessage()
{
ObjectMessage message = (ObjectMessage) jmsTemplate.receive("IUserQueue");
// ObjectMessage message = (ObjectMessage) jmsTemplate.receive("IUserTopic");
IUser iUser = null;
try
{
iUser = (IUser) message.getObject();
System.out.println("receiving message!");
}
catch (JMSException e)
{
e.printStackTrace();
}
return iUser;
}
}
4. The JUnit test case to verify the functionality
MessageProducerTest.java:
package com.sillycat.easymdbproxy.mdb;
import com.sillycat.core.BaseTestCase;
import com.sillycat.core.webservice.model.IUser;
public class MessageProducerTest extends BaseTestCase
{
MessageProducer messageProducer;
public void setUp() throws Exception
{
super.setUp();
messageProducer = (MessageProducer) ctx.getBean("messageProducer");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testSendMessage()
{
IUser iuser = new IUser();
iuser.setId(1);
iuser.setEmail("[email protected]");
messageProducer.sendMessage(iuser);
}
}
MessageConsumerTest.java:
package com.sillycat.easymdbproxy.mdb;
import com.sillycat.core.BaseTestCase;
import com.sillycat.core.webservice.model.IUser;
public class MessageConsumerTest extends BaseTestCase
{
MessageConsumer messageConsumer;
public void setUp() throws Exception
{
super.setUp();
messageConsumer = (MessageConsumer) ctx.getBean("messageConsumer");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testReceiveMessage()
{
IUser iUser = messageConsumer.receiveMessage();
assertNotNull(iUser);
System.out.println("id========" + iUser.getId() + "\nemail======" + iUser.getEmail());
}
}
References:
http://hi.baidu.com/luohuazju/blog/item/558938ec7f78092362d09fa3.html
http://hi.baidu.com/luohuazju/blog/item/1ef9350fead87de0ab6457e3.html
http://hi.baidu.com/luohuazju/blog/item/8f45d85459075b59564e00ec.html
http://blog.csdn.net/liuzhenwen/archive/2009/01/13/3768766.aspx
1. configure JMS in JBOSS
JBOSS_HOME/server/default/deploy/messaging/desinations-service.xml
add some content:
<!-- Register User Send/Receive Queue -->
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=IUserQueue">
<attribute name="JNDIName">queue/IUserQueue</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<!-- Register User Send/Receive Topic -->
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=IUserTopic">
<attribute name="JNDIName">topic/IUserTopic</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
2. Spring configuration file jms-easymdbproxy-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>XAConnectionFactory</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="destination" />
<!--
true for Topic
false for Queue
-->
<property name="pubSubDomain">
<value>false</value>
</property>
<!-- message waiting time(ms) -->
<property name="receiveTimeout">
<value>30000</value>
</property>
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<!--
<property name="jndiName">
<value>topic/IUserTopic</value>
</property>
<property name="jndiName">
<value>queue/IUserQueue</value>
</property>
-->
<property name="jndiName">
<value>queue/IUserQueue</value>
</property>
</bean>
<!-- message producer -->
<bean id="messageProducer" class="com.sillycat.easymdbproxy.mdb.MessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- message receiver -->
<bean id="messageConsumer" class="com.sillycat.easymdbproxy.mdb.MessageConsumer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
</beans>
3. Java class that implement the function
MessageProducer.java:
package com.sillycat.easymdbproxy.mdb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.sillycat.core.webservice.model.IUser;
public class MessageProducer
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final IUser iUser)
{
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
message.setObject(iUser);
System.out.println("send message success!!");
return message;
}
});
}
}
Java class that consumer the JMS, MessageConsumer.java:
package com.sillycat.easymdbproxy.mdb;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import org.springframework.jms.core.JmsTemplate;
import com.sillycat.core.webservice.model.IUser;
public class MessageConsumer
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public IUser receiveMessage()
{
ObjectMessage message = (ObjectMessage) jmsTemplate.receive("IUserQueue");
// ObjectMessage message = (ObjectMessage) jmsTemplate.receive("IUserTopic");
IUser iUser = null;
try
{
iUser = (IUser) message.getObject();
System.out.println("receiving message!");
}
catch (JMSException e)
{
e.printStackTrace();
}
return iUser;
}
}
4. The JUnit test case to verify the functionality
MessageProducerTest.java:
package com.sillycat.easymdbproxy.mdb;
import com.sillycat.core.BaseTestCase;
import com.sillycat.core.webservice.model.IUser;
public class MessageProducerTest extends BaseTestCase
{
MessageProducer messageProducer;
public void setUp() throws Exception
{
super.setUp();
messageProducer = (MessageProducer) ctx.getBean("messageProducer");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testSendMessage()
{
IUser iuser = new IUser();
iuser.setId(1);
iuser.setEmail("[email protected]");
messageProducer.sendMessage(iuser);
}
}
MessageConsumerTest.java:
package com.sillycat.easymdbproxy.mdb;
import com.sillycat.core.BaseTestCase;
import com.sillycat.core.webservice.model.IUser;
public class MessageConsumerTest extends BaseTestCase
{
MessageConsumer messageConsumer;
public void setUp() throws Exception
{
super.setUp();
messageConsumer = (MessageConsumer) ctx.getBean("messageConsumer");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testReceiveMessage()
{
IUser iUser = messageConsumer.receiveMessage();
assertNotNull(iUser);
System.out.println("id========" + iUser.getId() + "\nemail======" + iUser.getEmail());
}
}
References:
http://hi.baidu.com/luohuazju/blog/item/558938ec7f78092362d09fa3.html
http://hi.baidu.com/luohuazju/blog/item/1ef9350fead87de0ab6457e3.html
http://hi.baidu.com/luohuazju/blog/item/8f45d85459075b59564e00ec.html
http://blog.csdn.net/liuzhenwen/archive/2009/01/13/3768766.aspx