SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQ

本篇开始将具体介绍SpringBoot如何整合其它项目。java

如何建立SpringBoot项目

访问https://start.spring.io/。 git

依次选择构建工具Maven Project、语言java、Spring Boot版本2.0.5,点击Generate Project下载项目压缩包,解压后倒入到ide中便可。(idea集成了SpringBoot,可直接建立项目)

如何整合ActiveMQ

一、添加spring-boot-starter-activemq依赖github

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
复制代码

二、添加配置spring

spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
复制代码

三、添加SampleActiveMQApplication类apache

@SpringBootApplication
@EnableJms
public class SampleActiveMQApplication {
	@Bean
	public Queue queue() {
		return new ActiveMQQueue("sample.queue");
	}

	public static void main(String[] args) {
		SpringApplication.run(SampleActiveMQApplication.class, args);
	}

}
复制代码

四、添加Consumer类bash

@Component
public class Consumer {
	@JmsListener(destination = "sample.queue")
	public void receiveQueue(String text) {
		System.out.println(text);
	}

}
复制代码

五、添加Producer类tcp

@Component
public class Producer implements CommandLineRunner {
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	@Autowired
	private Queue queue;

	@Override
	public void run(String... args) throws Exception {
		send("Sample message");
		System.out.println("Message was sent to the Queue");
	}

	public void send(String msg) {
		this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
	}

}
复制代码

六、启动服务 控制台输出:ide

Message was sent to the Queue
Sample message
复制代码

以上例子使用的是SpringBoot内部的ActiveMQ,实际使用时确定会用外部的ActiveMQ。spring-boot

如何链接外部的ActiveMQ

一、添加依赖工具

<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>
复制代码

二、修改配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.close-timeout=5000
spring.activemq.in-memory=false
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100
spring.activemq.send-timeout=3000
复制代码

三、启动服务 首先,确保ActiveMQ已经启动。控制台输出:

Message was sent to the Queue
Sample message
复制代码

如何使用topic

一、在SampleActiveMQApplication中添加:

@Bean
    public Topic topic() {
        return new ActiveMQTopic("sample.topic");
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
复制代码

二、建立TopicConsumer类

@Component
public class TopicConsumer {

    @JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic1(String text) {
        System.out.println(text);
    }

    @JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic2(String text) {
        System.out.println(text);
    }
}
复制代码

三、建立TopicProducer类

@Component
public class TopicProducer implements CommandLineRunner {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    @Override
    public void run(String... args) throws Exception {
        send("Topic message");
        System.out.println("Message was sent to the Topic");
    }

    public void send(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }

}
复制代码

四、启动服务 控制台输出:

Message was sent to the Topic
Topic message
Topic message
复制代码

项目地址:SpringBoot2.0整合ActiveMQ


本篇到此结束,若是读完以为有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

相关文章
相关标签/搜索