mq消息队列
rabbitmq學習9:使用spring-amqp發(fā)送消息及同步接收消息通過對spring-amqp看重要類的認識,下面來通過spring-amqp的發(fā)送消息及同步接收消息是如何實現(xiàn)的。有興趣的朋友 可以去spring-amqp官網(wǎng)下載例子。先來看看HelloWorldConfiguration類Java代碼 收藏代碼
package org.springframework.amqp.helloworld; import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class HelloWorldConfiguration extends AbstractRabbitConfiguration { protected final String helloWorldQueueName = "hello.world.queue"; @Bean public ConnectionFactory connectionFactory() { SingleConnectionFactory connectionFactory = new SingleConnectionFactory( "localhost"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); return connectionFactory; } @Override public RabbitTemplate rabbitTemplate() { RabbitTemplate template = new RabbitTemplate(connectionFactory()); // The routing key is set to the name of the queue by the broker for the // default exchange. template.setRoutingKey(this.helloWorldQueueName); // // Where we will synchronously receive messages from template.setQueue(this.helloWorldQueueName); return template; } @Bean public Queue helloWorldQueue() { return new Queue(this.helloWorldQueueName); }
} 此類定義了ConnectionFactory 、RabbitTemplate 、Queue發(fā)送消息的程序如下:Java代碼 收藏代碼
package org.springframework.amqp.helloworld; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Producer { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class); AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); amqpTemplate.convertAndSend("Hello World"); System.out.println("Sent: Hello World"); } } 同步接收消息如下:Java代碼 收藏代碼
package org.springframework.amqp.helloworld; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Consumer { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class); AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); System.out.println("Received: " + amqpTemplate.receiveAndConvert()); } } 這個例子是Exchange類型為DirectExchange. routingkey的名稱默認為Queue的名稱。對于 HelloWorldConfiguration類的配置,也可以通過SPRING XML文件來配置。例如rabbitConfiguration.xmlJava代碼 收藏代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 創(chuàng)建connectionFactory --> <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory"> <constructor-arg value="localhost" /> <property name="username" value="guest" /> <property name="password" value="guest" /> </bean> <bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin"> <constructor-arg ref="connectionFactory" /> </bean> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="connectionFactory"></constructor-arg> <property name="queue" value="hello.world.queue"></property> <property name="routingKey" value="hello.world.queue"></property> </bean> <!-- 聲明Queue并設(shè)定Queue的名稱 --> <bean id="helloWorldQueue" class="org.springframework.amqp.core.Queue"> <constructor-arg value="hello.world.queue"></constructor-arg> </bean> </beans>
?
轉(zhuǎn)載于:https://www.cnblogs.com/leo3689/p/4209587.html
總結(jié)
- 上一篇: 图片压缩工具optipng/jpegop
- 下一篇: 去掉easyui data