spring-AMQP-RabbitMQ

1.spring integration rabbitMQ profile rabbitmq-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="127.0.0.1" port="5672" username="xuejianjun" password="xuejianjun"
virtual-host="/xuejianjun" />

<! - Rabbit defined template, and specifies the connection factory defined Exchange ->
<! - definition of a template, to send a message to the switch ->
<Rabbit: Template ID = "amqpTemplate" Connection-Factory = "The connectionFactory" Exchange = "fanoutExchange" />
<- send messages to the queue, need to be modified as follows:! ->
<- <Rabbit:! Template ID = "amqpTemplate" Connection-Factory = "the connectionFactory" queue = "column names "/> ->
<- <Rabbit:! Template ID =" amqpTemplate "Connection-Factory =" The connectionFactory "
Exchange =" fanoutExchange "routing-Key =" foo.bar "/> ->

<! - the MQ management, including queues, switches, etc. ->
<! - configuration management purposes, the following statement is to automatically queue, switches, etc. ->
<Rabbit: Connection-ADMIN = Factory " connectionFactory "/>

<! - definition of the queue, automatically declared ->
<Rabbit: Queue name = "myQueue" Auto-DECLARE = "to true" />

<! - definition of the switch, automatically declared, ->
<- Switch no! automatically present statement, the statement does not exist ->
<Rabbit: fanout-Exchange name = "fanoutExchange" Auto-dECLARE = "to true">
<Rabbit: Bindings>
<Rabbit: Queue Binding = "myQueue" />
</ Rabbit : Bindings>
</ Rabbit: fanout-Exchange>

<- <Rabbit:! Topic-Exchange name = "myExchange">
<Rabbit: Bindings>
<Rabbit: Queue Binding = "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="cn.itcast.rabbitmq.spring.Foo" />

</beans>

 

2. Test class

 

 SpringMain.java

================================================================================================================================

package cn.itcast.rabbitmq.spring;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMain {
public static void main(final String... args) throws Exception {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:spring/rabbitmq-context.xml");
//RabbitMQ模板
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
//发送消息
template.convertAndSend("Hello, world!");
Thread.sleep(1000);// 休眠1秒
ctx.destroy(); //容器销毁
}
}

=============================================================================================================================

Foo.java Note: Meaning Foo, is the meaning of distance, such as Chinese are: Joe Smith, John Doe, etc. 

package cn.itcast.rabbitmq.spring;

/ **
* consumer
* @author Zhijun
*
* /
public class Foo {

Specific method of performing service //
public void the listen (String foo) {
System.out.println ( "Consumer:" + foo);
}
}

Guess you like

Origin www.cnblogs.com/curedfisher/p/11896773.html