spring整合ActiveMQ及测试

第一步:引入依赖(使用Activemq引入三个依赖即可)

       <dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

第二步:编写配置文件
applicationContext-ActiveMQ.xml

<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.25.133:61616" />
	</bean>
	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="connectionFactory"
		  class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
	<!-- 配置生产者 -->
	<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
	<!--这个是队列目的地,点对点的 -->
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>spring-queue</value>
		</constructor-arg>
	</bean>
	<!--这个是主题目的地,一对多的 -->
	<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="topic" />
	</bean>

第三步:测试发送消息

 //拿到一个spring容器
        ApplicationContext applicationContext= new ClassPathXmlApplicationContext("classpath:spring/applicationContext-ActiveMQ.xml");
        //拿到jmsTemplate,用来接收和发送消息
        JmsTemplate jmsTemplate=applicationContext.getBean(JmsTemplate.class);
        //拿到Queue对象
        Destination destination= (Destination) applicationContext.getBean("queueDestination");
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("1111111111");
            }
        });

在这里插入图片描述
注意:这块不能倒错包,倒错包报错(Cannot resolve method ‘send(javax.print.attribute.standard.Destination, anonymous org.springframework.jms.core.MessageCreator)’)
导入的应该是这个包import javax.jms.Destination;
此时发送消息成功.
第四步:测试接收消息
配置监听类 我们通过自己写的MyMessageListener来实现监听。

<bean id="myMessageListener" class="test.MyMessageListener"/>

	<!-- 消息监听容器 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="queueDestination" />
		<property name="messageListener" ref="myMessageListener" />
	</bean>

监听实现类:


public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage= (TextMessage) message;
        String text = null;
        try {
            text = textMessage.getText();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        System.out.println(text);
    }
}

测试成功。
在接受测试时,一定要一直启动这个spring容器,要不监听类不起作用,可以这样实现

public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext= new ClassPathXmlApplicationContext("classpath:spring/applicationContext-ActiveMQ.xml");
        System.in.read();
    }

只要不输入,那么这个spring容器一直存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值