activemq spring 整合

最近的项目需目需要用到activemq ,看了些资料,自己也写了一个简单的demo

 

Sender用来发消息

Receiver用来接收消息同时他实现了MessageListener接口

 

 

费话不多说,上代码

applicationContext.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--创建连接工厂  加上failover就可以重连-->
    <bean id="connectionFactory"
        class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover:(tcp://localhost:61616)"></property>
    </bean>
    <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="HelloWorldQueue"></constructor-arg>
    </bean>
    <!---->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="defaultDestination" ref="destination"></property>
        <property name="receiveTimeout" value="600"></property>

    </bean>
    <bean id="sender" class="com.Sender">
        <property name="jmsTemplate" ref="jmsTemplate"></property>

    </bean>
    <bean id="receiver" class="com.Receiver">
        <property name="jmsTemplate" ref="jmsTemplate"></property>
    </bean>
    
    
	<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="destination" />
		<property name="messageListener" ref="receiver" />
	</bean>
</beans>

 

 看完这个,大家估计就都明白了,哥的demo就是这么简单

 

package com;

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

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class Sender {
    private JmsTemplate jmsTemplate;
    //getter and setter
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    
    public void sendInfo() {
        jmsTemplate.send(new MessageCreator() {
            public TextMessage createMessage(Session session) throws JMSException {
            	TextMessage message = session.createTextMessage(" hello ou ba");
                return message;
            }

        });
    }
}

 

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;

public class Receiver implements MessageListener{
	private JmsTemplate jmsTemplate;

	// getter and setter
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	/**
	 * 构造函数
	 */
	public Receiver() {
	}

	public void onMessage(Message obj) {
		TextMessage message = (TextMessage) obj;
		try {
		System.out.println("receiver message" + message.getText());
		} catch (JMSException e) {
			throw JmsUtils.convertJmsAccessException(e);
		}
	}

}

 

 

再来个启动

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SenderTest {
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-jms2.xml");
        Sender sender = (Sender) context.getBean("sender");
        sender.sendInfo();
        System.out.println("send finish");
    }
}

 

因为把sender和reciever 写在了一个demo里,所以一运行起来,就会打印出结果,呵呵

你可能感兴趣的:(activemq spring 整合)