博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot整合ActiveMQ
阅读量:6207 次
发布时间:2019-06-21

本文共 6570 字,大约阅读时间需要 21 分钟。

 

先建工程

..

..

..

..

..先看一下最终目录结构(实际上核心就是两个类,但是其他的多写写还是没有坏处的)

消息实体类

package com.example.demo.domain;import java.io.Serializable;import java.util.Date;public class Message implements Serializable {    private int id;    private String from;    private String to;    private String text;    private Date time;    public Message(int id, String from, String to, String text, Date time) {        this.id = id;        this.from = from;        this.to = to;        this.text = text;        this.time = time;    }    public Message() {    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getFrom() {        return from;    }    public void setFrom(String from) {        this.from = from;    }    public String getTo() {        return to;    }    public void setTo(String to) {        this.to = to;    }    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }    public Date getTime() {        return time;    }    public void setTime(Date time) {        this.time = time;    }    @Override    public String toString() {        return "Message{" +                "id=" + id +                ", from='" + from + '\'' +                ", to='" + to + '\'' +                ", text='" + text + '\'' +                ", time=" + time +                '}';    }}
View Code

 

核心:发送类

package com.example.demo.service;import com.example.demo.domain.Message;import org.springframework.stereotype.Service;@Servicepublic interface MsgService {    void addMessage(Message message);}

 

 实现:我们习惯把@Autowired注解在属性上,但是SpringBoot推荐注解在构造函数上

package com.example.demo.service.impl;import com.example.demo.domain.Message;import com.example.demo.service.MsgService;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Service;import javax.jms.Destination;@Servicepublic class MsgServiceImpl implements MsgService {    private final JmsTemplate jmsTemplate;    @Autowired    public MsgServiceImpl(JmsTemplate jmsTemplate) {        this.jmsTemplate = jmsTemplate;    }    @Override    public void addMessage(Message message) {        Destination destination = new ActiveMQQueue("my-msg");        jmsTemplate.convertAndSend(destination, message);    }}

 

 核心:接受类

package com.example.demo.service;import com.example.demo.domain.Message;import org.springframework.stereotype.Service;import javax.jms.JMSException;@Servicepublic interface ReceiveService {    void receiveMessage(Message message) throws JMSException;}

 

 实现:

package com.example.demo.service.impl;import com.example.demo.domain.Message;import com.example.demo.service.ReceiveService;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Service;import javax.jms.JMSException;@Servicepublic class ReceiveServiceImpl implements ReceiveService {    @JmsListener(destination = "my-msg")    @Override    public void receiveMessage(Message message) throws JMSException {        System.out.println("收到:" + message);    }}

 

 

配置文件:application.yml

spring:  freemarker:    template-loader-path: classpath:/templates/    suffix: .ftl    charset: UTF-8    content-type: text/html    cache: false  activemq:    broker-url: tcp://localhost:61616    user: admin    password: admin    pool:      enabled: false      max-connections: 50    packages:      trust-all: true #不配置此项,会报错  http://activemq.apache.org/objectmessage.html

 

 controller:

package com.example.demo.controller;import com.example.demo.domain.Message;import com.example.demo.service.MsgService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import java.util.Date;@RestControllerpublic class ProducerController {    private final MsgService msgService;    @Autowired    public ProducerController(MsgService msgService) {        this.msgService = msgService;    }    @RequestMapping("/index")    public ModelAndView index(){        return new ModelAndView("index");    }    @RequestMapping("/send")    public String send(Message message){        System.out.println("前台:" + message);        msgService.addMessage(message);        return "ok";    }}

 

类型转换器:这里前台传来的是时间戳,需要转换成Date

package com.example.demo.utils;import org.springframework.core.convert.converter.Converter;import org.springframework.stereotype.Component;import java.util.Date;@Componentpublic class TimeConverter implements Converter
{ @Override public Date convert(String str) { return new Date(Long.valueOf(str)); }}

 

前台:

    
Jms
<#---->

 

css

@charset "UTF-8";.msg-wrap{
width: 600px; margin: 25px auto; box-shadow: 0 0 10px #dc143c; border: 1px solid #f97991; border-radius: 3px;}.msg-wrap .form-group{
margin: 15px;}.msg-wrap .form-group .form-control{
width: 100%; height: 30px; border: 1px solid #ddd; border-radius: 3px; text-indent: 8px;}.msg-wrap .form-group .textarea{
display: block; font-size: 16px; color: #ab1123; padding: 8px; border: 1px solid #ddd; border-radius: 3px;}.btn{
display: inline-block; padding: 8px 20px; color: #fff; background-color: crimson; border-radius: 3px; border: 1px solid crimson; text-decoration: none;}

 

js

$(function(){    $('.btn').click(function(){        var id = new Date().getDate() + "" + new Date().getMinutes();        var from = $('input[name="from"]').val();        var to = $('input[name="to"]').val();        var text = $('textarea[name="text"]').val();        var time = new Date().getTime();        var url = "/send";        var args = {"id": id, "from": from, "to": to, "text": text, "time": time};        $.post(url, args, function(data){            $('input[name="from"]').val("");            $('input[name="to"]').val("");            $('textarea[name="text"]').val("");        });    });});

 

最后,运行项目,访问:http://localhost:8080/index

查看IDEA控制台

查看ActiveMQ控制台:http://localhost:8161/admin/queues.jsp

因为我测试了几次,所以有4条消息

 

转载于:https://www.cnblogs.com/LUA123/p/8468898.html

你可能感兴趣的文章
6月份美国域名总量新增近5.4万个 环比减少51%
查看>>
mysql数据迁移
查看>>
Eclipse常用快捷键
查看>>
我的友情链接
查看>>
CentOS系统根目录组织结构
查看>>
LNMP里常见的502问题
查看>>
数据镜像备份工具rsync
查看>>
JSP被编译后的Serverlet
查看>>
cacti 忘记密码的方法
查看>>
Css3之基础-5 Css 背景、渐变属性
查看>>
使用/proc/meminfo文件查看内存状态信息
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
项目案例分享四:DC升级后Sysvol停止复制,日志报13508
查看>>
查询优化器内核剖析第四篇:从一个实例看执行计划
查看>>
Python14 函数
查看>>
ambari 自定义组件安装
查看>>
Linux下查看TOMCAT控制台
查看>>
关于outlook签名图片大小的说明
查看>>
CentOS7下分布式文件系统FastDFS的安装 配置 (单节点)
查看>>