ActiveMQ(二):使用队列Queue方式发送消息

来源:互联网 发布:淘宝有没有分期付款的 编辑:程序博客网 时间:2024/04/29 02:56

代码示例:队列queue方式发送消息

项目目录

配置activeMQ服务信息与实现springjms的对应接口

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:task="http://www.springframework.org/schema/task"     xmlns:util="http://www.springframework.org/schema/util"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:p="http://www.springframework.org/schema/p"    xmlns:jaxrs="http://cxf.apache.org/jaxrs"     xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/task     http://www.springframework.org/schema/task/spring-task-3.0.xsd    http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-2.5.xsd    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd    http://cxf.apache.org/transports/http/configuration        http://cxf.apache.org/schemas/configuration/http-conf.xsd">    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://localhost:61616" />        <property name="userName" value="admin" />        <property name="password" value="admin" />    </bean>    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">        <property name="connectionFactory" ref="connectionFactory" />    </bean>    <bean id="mqDestination" class="org.apache.activemq.command.ActiveMQQueue">        <!-- 设置消息队列的名字 -->        <constructor-arg index="0" value="STATUS" />    </bean>    <bean id="MessageSenderBean" class="activeMQ.MessageSender">        <constructor-arg name="template">            <ref bean="jmsTemplate" />        </constructor-arg>        <constructor-arg name="destination">            <ref bean="mqDestination" />        </constructor-arg>    </bean>    <bean id="MessageReceiverBean" class="activeMQ.MessageReceiver">        <constructor-arg name="template">            <ref bean="jmsTemplate" />        </constructor-arg>        <constructor-arg name="destination">            <ref bean="mqDestination" />        </constructor-arg>    </bean>  </beans>

消息生产者

package activeMQ;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import javax.ws.rs.Path;import javax.ws.rs.Produces;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 session) throws JMSException{        System.out.println("MyMessageCreator n=" +n);        if(n == 9){            return session.createTextMessage("end");        }        str = str1 + n +str2;        return session.createTextMessage(str);    }}

发送消息方

package activeMQ;import java.util.ArrayList;import java.util.List;import javax.jms.Destination;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;public class MessageSender extends Thread{      private static JmsTemplate jmsTemplate;    private static Destination destination;    public MessageSender(JmsTemplate template,Destination destination){        this.jmsTemplate = template;        this.destination = destination;    }    public static void main(String args[]) throws Exception{        ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml");        MessageSender myMessageSender = (MessageSender)ctx.getBean("MessageSenderBean");        for(int i = 1; i < 100; i++){            System.out.println("发送 i=" + i);            MymessageCreator myMessageCreator = new MymessageCreator();            myMessageCreator.n = i;            jmsTemplate.send(destination,myMessageCreator);            sleep(1000);        }    }}

消息发送方在发送消息时会自动调用MymessageCreator实现的MessageCreator类中的createMessage()方法来创建会话session中发送的消息。

消息接收方

package activeMQ;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.TextMessage;import javax.sound.midi.SysexMessage;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;public class MessageReceiver {    private static JmsTemplate jmsTemplate;    private static Destination destination;    public MessageReceiver(JmsTemplate template,Destination destination){        this.jmsTemplate = template;        this.destination = destination;    }    public static void main(String args[]) throws Exception{        ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml");        MessageReceiver myMessageReceiver = (MessageReceiver)ctx.getBean("MessageReceiverBean");        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("程序退出了!");    }}

测试:
运行发送方MessageSender和接受方MessageReceiver(顺序随便,因为采用对列Queue的形式,发送的消息都会存在队列value=”STATUS”中,消息接收方启动之后就会去取存在队列中的消息,即使接收方启动晚,或者发送发关闭了,接收方都会正常接收完所有数据。)

消息发送方

消息接收方

0 0
原创粉丝点击