spring---消息订阅发布之JMS

  作为消息组件的元老JMS一直是分布式开发框架的基础,接下来我们一起看一下spring+JMS实现的消息组件:

编码

添加依赖jar包

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.12.3</version>
</dependency>

定义Receiver:

package jms;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils;

@Component
public class Receiver {
    @Autowired
    ConfigurableApplicationContext context;
    @JmsListener(destination="mailbox-destination",containerFactory="myJmsContainerFactory")
    public void receiveMessage(String message){ 
        System.out.println("Receive <" + message + ">");
        context.close();
    }
}

在这里定义订阅的topicmailbox-destination,接下来看一下运行的代码:

package jms;

import java.io.File;

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.util.FileSystemUtils;

@SpringBootApplication
public class Application {

    @Bean
    JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        MessageCreator messageCreator = new MessageCreator() {
             @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage("ping!");
                }
        };
          JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
            System.out.println("Sending a new message.");
            jmsTemplate.send("mailbox-destination", messageCreator);
    }
}

这里发送消息ping,一起看一下结果:

Sending a new message.
Received <ping!>

参考:https://spring.io/guides

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值