Spring Boot + activemq+定时(一)

开发工具:idea.2018.2.4
环境:JDK1.8
消息组件:activemq
框架:Spring Boot 微服务框架
服务器:centos6.8 虚拟机一台(非必须)
能搜到这篇文章的说明应该都知道activemq消息服务器的作用,这里就不做赘述了,如果心里还是很迷糊,推荐阅读该文章,自我感觉能阐述明白activemq
开始正文
首先我们需要引入activemq的包

        
            org.springframework.boot
            spring-boot-starter-activemq
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.projectlombok
            lombok
            true
        

其中spring-boot-starter-activemq时必须要的包,至于其他两个视情况而定,如果你需要用的webmvc,那web这个包必须引入,如果你打算定义实体,那么lombok这个用几个注解轻松的帮你省去大量的get,set,toString,有无参数的构造方法等。
接下来就是Spring Boot 的独有文件application.properties或者application.yml ,当然个人建议使用yml,树状结构,容易看清楚组织间关系。
下面附上全配置

spring:
  application:
    name: springboot-activemq-provider
  activemq:
    in-memory: false #是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
    broker-url: tcp://10.0.194.183:61616 #mq的服务器地址
    user: admin
    password: admin
    pool:
      enabled: false #是否用Pooledconnectionfactory代替普通的ConnectionFactory(是否启用连接池管理)
      max-connections: 100 # 设置最大连接数
      time-between-expiration-check: 60s #过期时间超时(超过)
      idle-timeout: 60s #连接空闲超时
      block-if-full: true #当连接请求和吃满时是否阻塞,设置false会抛出JMSException 异常
      block-if-full-timeout: -1s #如果池仍然满,则在抛出异常前阻塞时间。

  jms:
    pub-sub-domain: true #默认false 只提供queue模式,如果要开启topic,必须设置为true
server:
  port: 8088

注意上面配置中本打算使用连接池但出现了一些意外的错误,始终无法解决,这里先改为false,接下来会单出一篇来阐述这个错误,并附上解决方案。
上面配置中最主要的就是activemq.* 这部分 除了pool.* 其实初始大家只需要配置

activemq:
    in-memory: false #是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
    broker-url: tcp://IP:61616 #mq的服务器地址
    user: admin
    password: admin

这点代码就可以了,上面IP指的时你activemq启动的IP,不知道大家有没有注意到in-memory: false 这个配置,是Springboot给我们的福利,测试的时候,你懒得装activemq,那么你可以把这个值改为true,这样,Springboot就帮你启动一个这样的时Activemq实例,这也算是个福利。如果这里你选择了in-memory: true,那么IP就是你本机。

大家都知道activemq有点对点和发布与订阅两种模式,这里我们就展示一下队列的点对点和主题的发布订阅,
首先呢我们建立一个配置类,下面我把配置类的源码放在下面

package com.feitian.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;

/**
 * EnableScheduling 开启定时任务
 * 定义主题与队列名称
 */
@EnableScheduling
@Configuration
public class MainConfig {
    @Bean(value ="queue",name = "queue")
    public Destination queue(){
        return new ActiveMQQueue("MECHENIKE_queue");
    }
    @Bean(value ="topic",name = "topic")
    public Destination topic(){
        return new ActiveMQTopic("MECHENIKE_topic");
    }
}

下面就是我们的正戏:
发送一个消息

package com.feitian.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.jms.Destination;
import java.util.Date;


@Component
public class Producer {
    /**
     * Destination  具体发送到哪一个队列或者主题当中
     */


    @Resource(name = "queue")
    private Destination queue;

    @Resource(name = "topic")
    private Destination topic;

    /**
     * JmsMessagingTemplate 对JmsTemplate进行了封装
     */
    @Autowired(required=true)
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void covertAndSend(Destination destination,String msg){
        jmsMessagingTemplate.convertAndSend(destination,msg);
        System.out.println("send : "+msg);
    }
    @Scheduled(fixedDelay=5000)
    public void send(){
        this.covertAndSend(this.queue,"this fist queue 01"+new Date().getTime());
        this.covertAndSend(this.topic,"this first topic 02"+new Date().getTime());
    }
}

以上就是完整的生产消息端的代码了
下一篇会完整附上消费段的,如何实现点对点,发布与订阅

你可能感兴趣的:(springboot)