springboot 学习笔记(七)
程序员文章站
2022-04-14 17:07:48
(七)springboot整合activemq,消息消费,以及发送对象消息 1、springboot整合activemq发送消息,上一节已经介绍了,现在要对消息队列中的内容进行处理,下面写一个consumer类,要加上@component注解,springboot会自动加载,@jmslistener ......
(七)springboot整合activemq,消息消费,以及发送对象消息
1、springboot整合activemq发送消息,上一节已经介绍了,现在要对消息队列中的内容进行处理,下面写一个consumer类,要加上@component注解,springboot会自动加载,@jmslistener标注为消息监听器,监听的队列通过destination属性指定
@component public class testconsumer { @jmslistener(destination="test.queue") public void getmsg(string msg) { system.out.println("收到消息:"+msg); } }
2、之前已经向activemq发送了两条消息,这时重启项目,看一下控制台会有以下输出
3、因为业务需求,我们可能需要向消息队列中发送对象,在activemq中新建user.queue队列,直接上代码
(1)首先在service接口中新增一个方法public void sendmessage(destination destination,object object);
具体实现为
public void sendmessage(destination destination, object object) { jms.convertandsend(destination, object); }
(2)写一个user实体类,具体属性及get、set方法,要在消息队列中发送对象,要实现序列化
/** * */ package com.zc.app.test.domain; import java.io.serializable; import java.util.date; public class user implements serializable{ private static final long serialversionuid = 1l; private string name; private int age; private string pwd; private string phone; private date createtime; public int getage() { return age; } public void setage(int age) { this.age = age; } public string getpwd() { return pwd; } public void setpwd(string pwd) { this.pwd = pwd; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public date getcreatetime() { return createtime; } public void setcreatetime(date createtime) { this.createtime = createtime; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
(3)写一个userconsumer,使用objectmapper进行对象转换
/** * */ package com.zc.app.test.jms; import java.io.ioexception; import javax.jms.jmsexception; import javax.jms.objectmessage; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jms.annotation.jmslistener; import org.springframework.stereotype.component; import com.fasterxml.jackson.core.jsonparseexception; import com.fasterxml.jackson.databind.jsonmappingexception; import com.fasterxml.jackson.databind.objectmapper; import com.zc.app.test.domain.user; @component public class userconsumer { @autowired private objectmapper om; //实时监听队列 @jmslistener(destination="user.queue") public void receivequeue(objectmessage object) throws jsonparseexception, jsonmappingexception, ioexception, jmsexception { system.out.println(object.getobject().tostring()); user user = om.convertvalue(object.getobject(), user.class); system.out.println("收到对象为:"+user.getname()); } }
(4)进行对象传输,需要开启activemq开启包配置,要在application.properties添加spring.activemq.packages.trust-all=true
(5)在controller中写一下测试,上代码
@getmapping("sendobj") public string object() { user user = new user(); user.setage(18); user.setcreatetime(new date()); user.setname("张三"); user.setphone("1154421122"); user.setpwd("123456"); destination destination = new activemqqueue("user.queue"); msgservice.sendmessage(destination, user); return "send obj success"; }
4、启动程序,直接访问http://localhost:8080/msg/sendobj,输出如下内容
5、这样就可以设置vo等对象进行负责的业务处理,我们也可以用json格式来进行对象的传输,使用objectmapper来进行json与对象的转换,相对来说这样的方式更加安全。