ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  274 随笔 :: 1 文章 :: 48 评论 :: 101万 阅读
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
概述:

下面将介绍如何在Spring下集成ActiveMQ
消费者:同步接收;
目的地:Queue



环境:

共5个文件
  1. Receiver.java
  2. ReceiverTest.java
  3. Sender.java
  4. SenderTest.java
  5. applicationContext.xml
使用的jar包如下:


源文件:

Receiver.java
package com.ll.springActiveMQ2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;

public class Receiver {
    private JmsTemplate jmsTemplate;
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    /**
     * 构造函数
     */
    public Receiver() {
    }

    public String receiveMessage() {
        String my = "";
        MapMessage message = (MapMessage) jmsTemplate.receive();
        try {
            my = message.getString("lastname");
        } catch (JMSException e) {
            throw JmsUtils.convertJmsAccessException(e);
        }
        return my;
    }


}

ReceiverTest.java

package com.ll.springActiveMQ2;

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

public class ReceiverTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Receiver receiver = (Receiver) context.getBean("receiver");
		System.out.print("接收到消息:" + receiver.receiveMessage());
	}
}

Sender.java

package com.ll.springActiveMQ2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
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 JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    public void sendInfo() {
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                MapMessage message = session.createMapMessage();
                message.setString("lastname", "科比");
                return message;
            }

        });
    }
}

SenderTest.java

package com.ll.springActiveMQ2;

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

public class SenderTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sender sender = (Sender) context.getBean("sender");
        sender.sendInfo();
        System.out.println("消息已发送...");
    }
}


配置文件:

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

	<!--创建连接工厂 -->
	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616"></property>
	</bean>

	<!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic -->
	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="com.ll.springActiveMQ2"></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.ll.springActiveMQ2.Sender">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
	</bean>
	
	<!-- 接收者/消费者 -->
	<bean id="receiver" class="com.ll.springActiveMQ2.Receiver">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
	</bean>
</beans>



运行结果:













posted on   ssslinppp  阅读(391)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示