ActiveMQ+Spring3.0

来源:互联网 发布:淘宝申诉 编辑:程序博客网 时间:2024/06/05 02:35

一时兴起配置了一下apache-activemq-5.3.2与spring3.0组合发送与接收消息。由于以前没有用过activemq所以部分代码是参考的网上的。下面是具体的实现过程:

 

1.环境准备

 

eclipse3.6+apache-activemq-5.3.2+spring3.0

 

2.所需要的jar

 

activemq-all-5.3.2.jar

org.springframework.asm-3.0.3.RELEASE.jar

org.springframework.beans-3.0.3.RELEASE.jar

org.springframework.context-3.0.3.RELEASE.jar

org.springframework.core-3.0.3.RELEASE.jar

org.springframework.expression-3.0.3.RELEASE.jar

org.springframework.jms-3.0.3.RELEASE.jar

org.springframework.transaction-3.0.3.RELEASE.jar

 

3.spring配置文件applicationContext.xml

 

这时主要是配置activemq服务信息与实现springjms的对应接口

我把该文件放在test包下,与程序放在同一个包下了。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans> 
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value>
        </property>
    </bean>
 
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref bean="connectionFactory"/>
        </property>
    </bean>
 
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0">
            <value>MessageQueue</value>
        </constructor-arg>
    </bean>
</beans>

 

4.发送消息方MessageSender.java

 

package test;

import javax.jms.Destination;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class MessageSender extends Thread {

    public static void main(String args[]) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "test/applicationContext.xml" });
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");
        for (int i = 1; i < 10; i++) {
            MyMessageCreator mc = new MyMessageCreator();//生成消息
            mc.n = i;
            jmsTemplate.send(destination, mc);
            sleep(1000);//1秒后发送下一条消息
        }

    }

}

 

其中MyMessageCreator我稍稍自己封装了一下,其中的n变量只为测试用,所以我设置成了public类型。MyMessageCreator.java内容如下:

 

package test;

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

import org.springframework.jms.core.MessageCreator;

public class MyMessageCreator implements MessageCreator {
    public int n = 0;
    private static String str1 = "这个是第 ";
    private static String str2 = " 个测试消息!";
    private String str = "";

    @Override
    public Message createMessage(Session paramSession) throws JMSException {
        if (n == 9) {
            return paramSession.createTextMessage("end");//在这个例子中表示第9次调用时,发送结束消息
        }
        str = str1 + n + str2;
        return paramSession.createTextMessage(str);
    }

}

当n==9,也就是程序里发送第9次信息的时候,发送结束标志'end',这样,接收方在收到结束标志后,会退出程序,否则继续下一条消息。

 

5.接收方MessageReciver.java

 

package test;

import javax.jms.Destination;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class MessageReciver{

    public static void main(String args[]) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "test/applicationContext.xml" });
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");
      
        TextMessage msg = null;
        boolean isContinue = true;//是否继续接收消息
        while (isContinue) {
            msg = (TextMessage) jmsTemplate.receive(destination);
            System.out.println("收到消息 :" + msg.getText());
            if (msg.getText().equals("end")) {
                isContinue = false;
                System.out.println("收到退出消息,程序要退出!");

            }
        }
        System.out.println("程序退出了!");
    }
}

 

6.测试方法:

 

1)首先运行apache-activemq服务:找到E:/Server/apache-activemq-5.3.2/bin/activemq.bat并运行
2)执行接收端程序
3)执行发送端程序
4)其实接收和发送端程的执行序可以没有先后顺序,看各人的习惯了。

 

 

 

 

原创粉丝点击