SpringBoot整合ActiveMQ

先建工程

..

..

..

..

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

消息实体类

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 +
                ‘}‘;
    }
}

核心:发送类

package com.example.demo.service;

import com.example.demo.domain.Message;
import org.springframework.stereotype.Service;

@Service
public 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;

@Service
public 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;

@Service
public 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;

@Service
public 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;

@RestController
public 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;

@Component
public class TimeConverter implements Converter<String, Date> {
    @Override
    public Date convert(String str) {
        return new Date(Long.valueOf(str));
    }
}

前台:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jms</title>
    <link href="/css/index.css" rel="stylesheet" type="text/css">
    <script src="/script/jquery-3.3.1.min.js"></script>
    <script src="/script/index.js"></script>
    <#--<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>-->
</head>
<body>
    <div class="msg-wrap">
        <div class="form-group">
            <input class="form-control" type="text" name="from" placeholder="发件人">
        </div>
        <div class="form-group">
            <input class="form-control" type="text" name="to" placeholder="收件人">
        </div>
        <div class="form-group">
            <textarea class="textarea" name="text"></textarea>
        </div>
        <div class="form-group">
            <a href="javascript:void(0)" class="btn">发送</a>
        </div>
    </div>
</body>
</html>

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

时间: 2024-10-31 10:32:39

SpringBoot整合ActiveMQ的相关文章

Web项目容器集成ActiveMQ &amp; SpringBoot整合ActiveMQ

集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Listener监听ServletContext创建和销毁进行Broker的启动和销毁. 0.需要的jar包: 1.listener实现ServletContextListener接口 package cn.qlq.listener; import javax.servlet.ServletContextEv

解决Springboot整合ActiveMQ发送和接收topic消息的问题

环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> &l

SpringBoot整合ActiveMQ发送邮件

虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 1. 生产者1.1 引入maven依赖1.2 application.yml配置1.3 创建配置类ConfigQueue1.4 创建生产者类Producer1.5 启动类AppProducer2. 消费者2.1 引入maven依赖2.2 application.yml配置2.3 创建消费者类Consumer2.4 启动类AppConsumer3. 启动截图3.1 生产者截图3.2 消费者截图3.

SpringBoot整合ActiveMQ实现持久化

点对点(P2P) 结构 创建生产者和消费者两个springboot工程 导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 生产者 步骤一:application.properties文件 spring.activemq.brok

springboot整合activemq小demo

直接上干货... 1.首先配置pom.xml文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.

SpringBoot 整合 ActiveMq

消息队列,用来处理开发中的高并发问题,通过线程池.多线程高效的处理并发任务. 首先,需要下载一个ActiveMQ的管理端:我本地的版本是 activemq5.15.8,打开activemq5.15.8\bin\win64\wrapper.exe客户端,可以根据localhost:端口号,访问ActiveMQ的管理界面.默认的用户名.密码都是admin. (一)pom 文件中添加 ActiveMq 依赖 <dependency> <groupId>org.apache.activem

springboot 整合ActiveMq

pom.xml <!-- 配置ActiveMQ启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 创建消息队列 //创建队列 @Bean public Queue queue(){ return new Acti

SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

https://www.cnblogs.com/xuyiqing/p/10851859.html https://www.cnblogs.com/leeSmall/p/8721556.html https://www.cnblogs.com/linyufeng/p/9885645.html 原文地址:https://www.cnblogs.com/418836844qqcom/p/11540020.html

SpringBoot整合ActiveMQ开启持久化

1.开启队列持久化 只需要添加三行代码 jmsTemplate.setDeliveryMode(2); jmsTemplate.setExplicitQosEnabled(true); jmsTemplate.setDeliveryPersistent(true); 2. 开启主题持久化,启动类添加如下配置 @Bean(name = "topicListenerFactory") public JmsListenerContainerFactory<DefaultMessageL