java 代码: Hello.java
- import java.io.Serializable;
- /**Hello.java用来传递JAVA对象
- * Author: cjp
- * Date: 2005-11-8
- * Time: 22:24:02
- */
- public class Hello implements Serializable {
- private String id;
- private Hello hello;
- private PointList pointList;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public Hello getHello() {
- return hello;
- }
- public void setHello(Hello hello) {
- this.hello = hello;
- }
- }
java 代码:SpringTest .java
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
- import javax.jms.*;
- /**
- *发送JMS消息
- */
- public class SpringTest extends AbstractDependencyInjectionSpringContextTests
- {
- protected String[] getConfigLocations()
- {
- return new String[]{"file:D:\\wosame\\test\\com\\wosame\\room\\jms\\jms.xml"};
- }
- public void testSendMessage() throws Exception
- {
- JmsTemplate jmsTemplate = (JmsTemplate) applicationContext.getBean("jmsTemplate");
- jmsTemplate.send(new MessageCreator()
- {
- public Message createMessage(Session session) throws JMSException
- {
- ObjectMessage message=session.createObjectMessage();
- Hello hello=new Hello();
- hello.setId("test");
- message.setObject(hello);
- return message;
- }
- });
- }
- }
java 代码
- /**
- 处理JMS消息
- */
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import javax.jms.*;
- public class HelloMDP implements MessageListener
- {
- protected Log log = LogFactory.getLog(HelloMDP.class);
- public void onMessage(Message message)
- {
- try
- {
- ObjectMessage objMessage = (ObjectMessage) message;
- Hello hello= (Hello) objMessage.getObject();
- System.out.println("hello.getId() = " + hello.getId());
- } catch (JMSException e)
- {
- log.error("Parse failed", e);
- }
- }
- }
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
- "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--嵌入式的JMS连接,也就是跟随JVM一起启动,可以参看activemq的文档-->
- <bean id="connectionFactory" class="org.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost"/>
- <property name="useEmbeddedBroker" value="true"/>
- </bean>
- <!--消息监听器,也就是消息的具体的处理器-->
- <bean id="HelloMDP" class="HelloMDP"/>
- <!--jms监听需要JTA容器的支持-->
- <bean id="activeMQContainer" class="org.activemq.jca.JCAContainer">
- <property name="workManager">
- <bean id="workManager" class="org.activemq.work.SpringWorkManager"/>
- </property>
- <property name="resourceAdapter">
- <bean id="activeMQResourceAdapter" class="org.activemq.ra.ActiveMQResourceAdapter">
- <property name="serverUrl" value="vm://localhost"/>
- </bean>
- </property>
- </bean>
- <!--消息的消费者,也就是将监听器与具体的队列关联-->
- <bean id="HelloQueueConsumer" factory-method="addConnector" factory-bean="activeMQContainer">
- <property name="activationSpec">
- <bean class="org.activemq.ra.ActiveMQActivationSpec">
- <property name="destination" value="Hello.Queue"/>
- <property name="destinationType" value="javax.jms.Queue"/>
- </bean>
- </property>
- <property name="ref" value="HelloMDP"/>
- </bean>
- <!--spring的JMS template,用来发送JMS消息到指定的队列-->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="defaultDestinationName" value="Hello.Queue"/>
- <property name="connectionFactory" ref="connectionFactory"/>
- </bean>
- </beans>