rabbitMq与spring的整合
来源:互联网 发布:八字反推软件 编辑:程序博客网 时间:2024/05/17 06:07
rabbitMq与spring的整合
- 本文所涉及字符串均可用ioc注入,只是我懒得写……
添加mvn库
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.3.5.RELEASE</version> </dependency>
添加connectionFactory的bean
<rabbit:connection-factory id="connectionFactory" host="localhost" publisher-confirms="true" virtual-host="/" username="admin" password="admin" />
- 详细配置说明参见静态网页
创建发送服务
@Servicepublic class SendService { private static final String EXCHANGE_NAME = "topic_logs"; @Autowired//这里注入了工厂 private ConnectionFactory connectionFactory; public void sendMessage() throws IOException, TimeoutException { Connection connection = null; try { connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); // 待发送的消息 String[] routingKeys = new String[] { "quick.blue.rabbit", "quick.red.rabbit" }; // 发送消息 for (String severity : routingKeys) { String message = "From " + severity + " routingKey' s message!"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println("TopicSend Sent '" + severity + "':'" + message + "'"); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.close();//必须关闭connection,来结束发送线程 } } } public ConnectionFactory getConnectionFactory() { return connectionFactory; } public void setConnectionFactory(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; }}
创建接受线程
@Servicepublic class RedListener implements Runnable {//另开线程监听队列 private static final String EXCHANGE_NAME = "topic_logs"; @Autowired//注入工厂 private ConnectionFactory connectionFactory; @Override public void run() {//线程主方法 try { Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); // 路由关键字 String[] routingKeys = new String[] { "*.red.*" }; // 绑定路由关键字 for (String bindingKey : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println("redTopic exchange:" + EXCHANGE_NAME + ", queue:" + queueName + ", BindRoutingKey:"+ bindingKey); } System.out.println("red [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("red Received '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public ConnectionFactory getConnectionFactory() { return connectionFactory; } public void setConnectionFactory(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; }}
主线程
public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); BlueListener blueListener = ctx.getBean(BlueListener.class); blueListener.run(); RedListener redListener = ctx.getBean(RedListener.class); redListener.run(); SendService send = ctx.getBean(SendService.class); send.sendMessage(); }
阅读全文
0 0
- rabbitMq与spring的整合
- RabbitMQ与Spring的整合
- 【RabbitMQ】RabbitMQ与Spring整合
- rabbitmq与spring整合
- RabbitMQ 与 Spring 整合使用
- RabbitMQ实践--与Spring的简单整合操作
- rabbitmq(七)与spring整合快速入门
- 消息队列 RabbitMQ 与 Spring 整合使用
- Rabbitmq 整合Spring,SpringBoot与Docker
- 消息队列 RabbitMQ 与 Spring 整合使用
- 消息队列 RabbitMQ 与 Spring 整合使用
- 消息队列 RabbitMQ 与 Spring 整合使用
- Spring Mvc整合Rabbitmq的简单例子
- Spring整合Rabbitmq
- Spring整合Rabbitmq
- rabbitMq入门,整合spring
- spring rabbitmq 整合配置
- rabbitmq整合spring
- 计算圆和长方形的面积
- js严格模式
- 面向对象的设计模式及魔术函数
- C# 系统应用之通过注册表获取USB使用记录
- ImageLoader缓存一个网址图片并存到SD卡上
- rabbitMq与spring的整合
- 内存分配策略
- QoS基础及技术原理——2
- 欢迎使用CSDN-markdown编辑器
- XListView上拉加载下拉刷新
- python发送各类邮件
- spring data jpa 1.9/1.10 复杂sql分页查询
- NOIp2000 进制转换
- Go语言接口interface