这个用了很多的时间都搞得成功,参考了不同的书籍,最后都成功了,在这里我把我的心得在这里写写
首先,下载ActiveMq5.1,大家可以在官方网站下载:当大家下载解压后,就将文件夹的的包COPY到web project下的lib文件夹里,需要拷贝的以下图的包(j2ee和jre除外)
好了,现在开始写配置文件了。在src文件夹下新建一spring配置文件名为:applicationContext.xml。其代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property> </bean> <bean id="jmsQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/Queue"></property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsConnectionFactory"></property> <property name="defaultDestination" ref="jmsQueue"></property> </bean> <bean id="sender" class="message.Sender"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> <bean id="receive" class="message.Receiver"></bean> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory"></property> <property name="destination" ref="jmsQueue"></property> <property name="messageListener" ref="receive"></property> </bean> </beans>
这里中,其中就有一个地方花了我很多时间找资料。这里是用了ActiveMq的方式来配置Spring的配置文件,其中引入了xsd的文件。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> ................... <beans>
由于面的配置文件中使用了jndi,所以也得在Tomcat目录下的conf/context.xml配置,需加入
<Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="vm://localhost" brokerName="LocalActiveMQBroker"/> <Resource name="jms/Queue" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="my Queue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="FOO.BAR"/>
在这里我分别配置了jmsConnectionFactory和jms点对点
之后,就由消息生产者的代码
package message; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class Sender { private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void send(final String text){ System.out.println("---Send:"+text); jmsTemplate.send(new MessageCreator(){ public Message createMessage(Session arg0) throws JMSException { // TODO Auto-generated method stub return arg0.createTextMessage(text); } }); } }
消息接收者
package message; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class Receiver implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage text = (TextMessage) message; try { System.out.println("Receive:" + text.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
这里分别帖了最简单的JMS配置,希望对初入门的有用。