使用spring-amqp结合使用rabbitmq

当前位置 : 首页 > 网页制作 > CSS > 使用spring-amqp结合使用rabbitmq

使用spring-amqp结合使用rabbitmq

来源: 作者: 时间:2016-01-28 09:27
maven 依赖包配置如下:[html] dependencies dependency groupIdorg springframework amqp groupId artifactIdspring-rabbit artifactId version1 2 0 RELEASE
maven 依赖包配置如下:
[]  
<dependencies>  
    <dependency>  
        <groupId>org.springframework.amqp</groupId>  
        <artifactId>spring-rabbit</artifactId>  
        <version>1.2.0.RELEASE</version>  
    </dependency>  
</dependencies>  
 
通过spring 获得到连接,并发送消息
[java]  
public static void main(final String... args) throws Exception {  
  
    AbstractApplicationContext ctx =  
        new ClassPathXmlApplicationContext("context.xml");  
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);  
    template.convertAndSend("Hello, world!");  
    Thread.sleep(1000);  
    ctx.destroy();  
}  
 
context.xml 配置如下:
[html]  
<rabbit:connection-factory id="connectionFactory" />  
  
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"  
    exchange="myExchange" routing-key="foo.bar"/>  
  
<rabbit:admin connection-factory="connectionFactory" />  
  
<rabbit:queue name="myQueue" />  
<!--路由设置 将队列绑定,属于topic类型-->  
<rabbit:topic-exchange name="myExchange">  
    <rabbit:bindings>  
        <rabbit:binding queue="myQueue" pattern="foo.*" />  
    </rabbit:bindings>  
</rabbit:topic-exchange>  
<!-- 监听类设置-->  
  
<rabbit:listener-container connection-factory="connectionFactory">  
    <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />  
</rabbit:listener-container>  
  
<bean id="foo" class="foo.Foo" />  
 
Foo 类 此类用于监听消息
[java]  
public class Foo {  
  
    public void listen(String foo) {  
        System.out.println(foo);  
    }  
}  
 
Tag:
网友评论

<