• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

使用spring-amqp结合使用rabbitmq

maven 依赖包配置如下:

 

<dependencies>
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>
</dependencies>


通过spring 获得到连接,并发送消息

 

 

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 配置如下:

 

 

<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 类 此类用于监听消息

 

 

public class Foo {

    public void listen(String foo) {
        System.out.println(foo);
    }
}


 

 

posted @ 2013-09-26 22:53  Class Xman  阅读(548)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3