1、首先,该文章主要讲解内容,是如何从spring中继承activemq。我选择的activemq是本机的windows环境,版本是apache-activemq-5.5.1。


2、spring的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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />

<bean id="connectionFactory"
class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>

<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="spitter.queue"></constructor-arg>
</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>

<bean id="main" class="com.test.activemq.SendBySpring">
</bean>

<bean id="receive" class="com.test.activemq.ReceiveBySpring">
</bean>

</beans>


3、发送类

package com.test.activemq;

import javax.jms.JMSException;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.test.bean.Message;


public class SendBySpring {

@Autowired
JmsTemplate jmsTemplate;
public void sending(final String mes)
{
jmsTemplate.send("test",
new MessageCreator()
{

public javax.jms.Message createMessage(Session session)
throws JMSException {

return session.createTextMessage(mes);
}

}

);

}
public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(
"amq.xml");
SendBySpring main= (SendBySpring) ctx.getBean("main");
main.sending("hello");

}

}


4、接收类

package com.test.activemq;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class ReceiveBySpring {


@Autowired
JmsTemplate jmsTemplate;

public String get() throws JMSException
{
TextMessage receivedMessage=
(TextMessage ) jmsTemplate.receive("test");
return receivedMessage.getText();

}
public static void main(String[] args) throws JMSException {

ApplicationContext ctx = new ClassPathXmlApplicationContext(
"amq.xml");
ReceiveBySpring receive=(ReceiveBySpring) ctx.getBean("receive");
System.out.println(receive.get());

}

}


5、启动activemq

spring集成activemq消息中间件_spring


双击运行,会出现如下的界面

spring集成activemq消息中间件_xml_02


注意,如果没有出现这个界面,或者出现界面显示抛出异常,说明没有正确启动activemq,需要检查或者更换版本,注意,activemq有些版本有本身的bug,会经常导致启动异常。


6、输出结果

先运行发送类,再运行接收类,就能够看到接收类的输出结果,就是从activemq中取得了消息,并且打印显示出来。

spring集成activemq消息中间件_java_03




附件:所用到的jar包

spring集成activemq消息中间件_xml_04