spring_rabbitmq_amqp学习笔记
2018-03-29 20:02
309 查看
记一记:
加入相关依赖后。。。
【简单的】
相关的spring配置文件:T3.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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="/ywj" username="ywj" password="0"/>
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jackson2JsonMessageConverter"/>
<rabbit:admin connection-factory="connectionFactory"/>
<!--一个队列-->
<rabbit:queue name="testQueue" auto-delete="true" durable="false"/>
<!--json序列化-->
<bean id="jackson2JsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>
</beans>相关的java代码:package com.example.java.t0;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("testQueue", new Order(1L,"order0"));
System.out.println("发送成功");
Thread.sleep(1000);
Object o = template.receiveAndConvert("testQueue");
Order order = (Order) o;
System.out.println("接收:"+order.getName());
ctx.destroy();
}
}
【work模式】:
spring配置文件如上的T3.xml.
发送者:package com.example.java.t0;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
for(int i = 0; i < 20; i ++){
template.convertAndSend("testQueue", "msg "+i+" "+System.currentTimeMillis());
}
System.out.println("发送成功");
ctx.destroy();
}
}
接收者1:package com.example.java.t0;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3_1 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
while (true){
Thread.sleep(1000);
System.out.println("1接收:"+template.receiveAndConvert("testQueue"));
}
}
}
接收者2同上
【topic模式】:
spring.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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="/ywj" username="ywj" password="0"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="topicExchange" routing-key="nokia.8plus"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="appleQueue" auto-delete="true" durable="false"/>
<rabbit:queue name="nokiaQueue" auto-delete="true" durable="false"/>
<rabbit:topic-exchange name="topicExchange" auto-delete="true" durable="false">
<rabbit:bindings>
<rabbit:binding queue="appleQueue" pattern="apple.*" />
<rabbit:binding queue="nokiaQueue" pattern="nokia.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="apple" method="listen" queue-names="appleQueue" />
<rabbit:listener ref="nokia" method="listen" queue-names="nokiaQueue" />
</rabbit:listener-container>
<bean id="apple" class="com.example.java.t0.Apple" />
<bean id="nokia" class="com.example.java.t0.Nokia" />
</beans>发送者:package com.example.java.t0;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("context.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("买买买!");
Thread.sleep(1000);
ctx.destroy();
}
}
接收者apple:package com.example.java.t0;
public class Apple {
public void listen(String msg)
{
System.out.println("Apple接收到的信息:"+msg);
}
}
接收者nokia:package com.example.java.t0;
public class Nokia {
public void listen(String msg)
{
System.out.println("Nokia接收到的信息:"+msg);
}
}
spring.xml中可以去掉:<rabbit:template/>中的routing-key,在发送中设置:template.setRoutingKey("apple.8plus");
先记这么多先。。。
加入相关依赖后。。。
【简单的】
相关的spring配置文件:T3.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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="/ywj" username="ywj" password="0"/>
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jackson2JsonMessageConverter"/>
<rabbit:admin connection-factory="connectionFactory"/>
<!--一个队列-->
<rabbit:queue name="testQueue" auto-delete="true" durable="false"/>
<!--json序列化-->
<bean id="jackson2JsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>
</beans>相关的java代码:package com.example.java.t0;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("testQueue", new Order(1L,"order0"));
System.out.println("发送成功");
Thread.sleep(1000);
Object o = template.receiveAndConvert("testQueue");
Order order = (Order) o;
System.out.println("接收:"+order.getName());
ctx.destroy();
}
}
【work模式】:
spring配置文件如上的T3.xml.
发送者:package com.example.java.t0;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
for(int i = 0; i < 20; i ++){
template.convertAndSend("testQueue", "msg "+i+" "+System.currentTimeMillis());
}
System.out.println("发送成功");
ctx.destroy();
}
}
接收者1:package com.example.java.t0;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T3_1 {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("T3.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
while (true){
Thread.sleep(1000);
System.out.println("1接收:"+template.receiveAndConvert("testQueue"));
}
}
}
接收者2同上
【topic模式】:
spring.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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="/ywj" username="ywj" password="0"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="topicExchange" routing-key="nokia.8plus"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="appleQueue" auto-delete="true" durable="false"/>
<rabbit:queue name="nokiaQueue" auto-delete="true" durable="false"/>
<rabbit:topic-exchange name="topicExchange" auto-delete="true" durable="false">
<rabbit:bindings>
<rabbit:binding queue="appleQueue" pattern="apple.*" />
<rabbit:binding queue="nokiaQueue" pattern="nokia.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="apple" method="listen" queue-names="appleQueue" />
<rabbit:listener ref="nokia" method="listen" queue-names="nokiaQueue" />
</rabbit:listener-container>
<bean id="apple" class="com.example.java.t0.Apple" />
<bean id="nokia" class="com.example.java.t0.Nokia" />
</beans>发送者:package com.example.java.t0;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("context.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("买买买!");
Thread.sleep(1000);
ctx.destroy();
}
}
接收者apple:package com.example.java.t0;
public class Apple {
public void listen(String msg)
{
System.out.println("Apple接收到的信息:"+msg);
}
}
接收者nokia:package com.example.java.t0;
public class Nokia {
public void listen(String msg)
{
System.out.println("Nokia接收到的信息:"+msg);
}
}
spring.xml中可以去掉:<rabbit:template/>中的routing-key,在发送中设置:template.setRoutingKey("apple.8plus");
先记这么多先。。。
相关文章推荐
- rabbitmq学习9:使用spring-amqp发送消息及同步接收消息
- RabbitMQ学习(八)之spring-amqp的重要类的认识
- [RabbitMQ]13_RabbitMQ学习之spring-amqp的重要类的认识
- rabbitmq学习8:spring-amqp的重要类的认识
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- rabbitmq学习9:使用spring-amqp发送消息及同步接受消息
- 使用php-amqplib连接rabbitMQ 学习笔记及总结
- springboot学习笔记-6 springboot整合RabbitMQ
- rabbitmq学习以及spring项目spring-amqp配置
- Spring学习笔记3之消息队列(rabbitmq)发送邮件功能
- 【推荐】springboot学习笔记-6 springboot整合RabbitMQ
- RabbitMQ学习笔记十:RabbitMQ与Spring 的整合
- RabbitMQ学习之spring-amqp的重要类的认识
- RabbitMQ学习之spring-amqp的重要类的认识
- RabbitMQ学习笔记五:Exchange的学习(3)
- Spring4学习笔记-AOP前传之动态代理
- Spring学习笔记整理——注解
- SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇日期格式转化报400错误解决方法
- Spring入门学习笔记(3)——事件处理类
- WebService学习笔记-添加拦截器到基于Spring的Webservice