Spring Boot的JMS发送和接收队列消息,基于ActiveMQ

return “SimpleMsg{” +

“to='” + to + ‘’’ +

“, body='” + body + ‘’’ +

‘}’;

}

}

SimpleMsg将作为spring boot程序中消息的实体传输。

定义一个spring boot的消息接收器:

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class MsgReceiver {

//destination的值和在application里面发送制定的目的地值需要相同才能收到

//containerFactory的变量名对应在app里面定义的JmsListenerContainerFactory

@JmsListener(destination = “@zhangphil”, containerFactory = “myAppContainerFactory”)

public void receiveMessage(SimpleMsg msg) {

System.out.println(“收到消息->”);

System.out.println(msg);

System.out.println(“收到消息<-”);

}

}

完成spring boot的应用application:

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.Bean;

import org.springframework.jms.annotation.EnableJms;

import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.jms.support.converter.MappingJackson2MessageConverter;

import org.springframework.jms.support.converter.MessageConverter;

import org.springframework.jms.support.converter.MessageType;

@SpringBootApplication

@EnableJms

public class SpringJmsApplication {

@Bean

public JmsListenerContainerFactory<?> myAppContainerFactory(ConnectionFactory connectionFactory,

DefaultJmsListenerContainerFactoryConfigurer configurer) {

DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

configurer.configure(factory, connectionFactory);

return factory;

}

@Bean

public MessageConverter jmsMessageConverter() {

MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();

converter.setTargetType(MessageType.TEXT);

converter.setTypeIdPropertyName(“_type”);

return converter;

}

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(SpringJmsApplication.class, args);

JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

SimpleMsg msg = new SimpleMsg(“zhangfly”, “Hello,world!”);

System.out.println(“发送消息:” + msg);

jmsTemplate.convertAndSend(“@zhangphil”, msg);

System.out.println(“发送结束”);

}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。

image

上述的面试题答案都整理成文档笔记。 也还整理了一些面试资料&最新2021收集的一些大厂的面试真题(都整理成文档,小部分截图)

image

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
图)

[外链图片转存中…(img-JQ9i59S3-1712071774036)]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值