您当前的位置: 首页 >  中间件

消息中间件系列教程(02) -ActiveMQ -安装&入门案例

杨林伟 发布时间:2019-12-06 11:09:58 ,浏览量:2

引言

代码已提交至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringBoot-ActiveMQ-Demo

ActiveMQ在Windows和Linux下的安装之前有讲过:

  • 《Linux下ActiveMQ的下载与安装》
  • 《Window下ActiveMQ的下载与安装》

安装本文就不在讲解了,直接根据需求按以上两篇文章来安装。本文主要来讲解SpringBoot整合ActiveMQ。下面来讲解集成的步骤:

1. 提供者项目

step1:新建提供者项目(ActiveMQ-Producer)

step2:添加maven依赖


	org.springframework.boot
	spring-boot-starter-parent
	1.5.4.RELEASE
	 


	UTF-8
	UTF-8
	1.8


	
		org.springframework.boot
		spring-boot-starter
	
	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.springframework.boot
		spring-boot-starter-test
		test
	
	
		org.springframework.boot
		spring-boot-starter-activemq
	


	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	

step3:配置application.yml

server:
  port: 8080
spring:
  activemq:
    broker-url: tcp://192.168.162.131:61616
    user: admin
    password: admin
queue: springboot-queue

step4:创建QueueConfig

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jms.Queue;

@Configuration
public class QueueConfig {
    @Value("${queue}")
    private String queue;

    @Bean
    public Queue logQueue() {
        return new ActiveMQQueue(queue);
    }
}

step5:创建Producer

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component
@EnableScheduling
public class Producer {
    
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    
    @Autowired
    private Queue queue;

    @Scheduled(fixedDelay = 5000)
    public void send() {
        jmsMessagingTemplate.convertAndSend(queue, "测试消息队列" + System.currentTimeMillis());
    }
}

step6:启动类

@SpringBootApplication
@EnableScheduling
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
2. 消费者项目

step1:新建消费者项目(ActiveMQ-Consumer)

step2:添加maven依赖



	org.springframework.boot
	spring-boot-starter-parent
	2.0.1.RELEASE



	
		
			org.springframework.cloud
			spring-cloud-dependencies
			Finchley.M7
			pom
			import
		
	


	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		org.springframework.boot
		spring-boot-starter-activemq
	



	
		spring-milestones
		Spring Milestones
		https://repo.spring.io/libs-milestone
		
			false
		
	

step3:配置application.yml

server:
  port: 8081
spring:
  activemq:
    broker-url: tcp://192.168.162.131:61616
    user: admin
    password: admin
queue: springboot-queue

step4:创建消费者Consumer

@Component
public class Consumer {

    @JmsListener(destination = "${queue}")
    public void receive(String msg) {
        System.out.println("监听器收到msg:" + msg);
    }
}

step5:启动类

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
3. 测试

step1.启动ActiveMQ服务:

/usr/local/apache-activemq-5.15.10/bin/activemq start

在这里插入图片描述 step2.浏览器访问:http://192.168.162.131:8161/admin(账号:admin、密码:admin),可以看到ActiveMQ服务开启成功。 在这里插入图片描述 step3.启动生产者和消费者项目,可以看到生产者和消费者都注册到了ActiveMQ服务上了。 在这里插入图片描述 step4.在消费者的控制台,因为提供者是定时发送消息的,所以在消费者控制台可以看到消息接收: 在这里插入图片描述 成功!

关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 2浏览

    0关注

    3279博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.4177s