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

貳級天災發表於2018-09-30

本篇開始將具體介紹SpringBoot如何整合其它專案。

如何建立SpringBoot專案

訪問https://start.spring.io/。

SpringBoot2.0應用(二):SpringBoot2.0整合ActiveMQ
依次選擇構建工具Maven Project、語言java、Spring Boot版本2.0.5,點選Generate Project下載專案壓縮包,解壓後倒入到ide中即可。(idea整合了SpringBoot,可直接建立專案)

如何整合ActiveMQ

1、新增spring-boot-starter-activemq依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
複製程式碼

2、新增配置

spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
複製程式碼

3、新增SampleActiveMQApplication類

@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);
	}

}
複製程式碼

4、新增Consumer類

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

}
複製程式碼

5、新增Producer類

@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);
	}

}
複製程式碼

6、啟動服務 控制檯輸出:

Message was sent to the Queue
Sample message
複製程式碼

以上例子使用的是SpringBoot內部的ActiveMQ,實際使用時肯定會用外部的ActiveMQ。

如何連線外部的ActiveMQ

1、新增依賴

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>
複製程式碼

2、修改配置

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
複製程式碼

3、啟動服務 首先,確保ActiveMQ已經啟動。控制檯輸出:

Message was sent to the Queue
Sample message
複製程式碼

如何使用topic

1、在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;
    }
複製程式碼

2、建立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);
    }
}
複製程式碼

3、建立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);
    }

}
複製程式碼

4、啟動服務 控制檯輸出:

Message was sent to the Topic
Topic message
Topic message
複製程式碼

專案地址:SpringBoot2.0整合ActiveMQ


本篇到此結束,如果讀完覺得有收穫的話,歡迎點贊、關注、加公眾號【貳級天災】,查閱更多精彩歷史!!!

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

相關文章