-
-
org.springframework -
spring-context -
5.1.7.RELEASE -
-
-
org.springframework.amqp -
spring-rabbit -
2.1.8.RELEASE -
-
-
junit -
junit -
4.12 -
-
-
org.springframework -
spring-test -
5.1.7.RELEASE -
-
-
-
org.apache.maven.plugins -
maven-compiler-plugin -
3.8.0 -
-
-
1.8 -
-
-
- rabbitmq.host=43.143.246.208
- rabbitmq.port=5672
- rabbitmq.username=root
- rabbitmq.password=root
- rabbitmq.virtual-host=/itcast
- "1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:rabbit="http://www.springframework.org/schema/rabbit"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/rabbit
- http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
-
-
"classpath:rabbitmq.properties"/> -
-
-
"connectionFactory" host="${rabbitmq.host}" - port="${rabbitmq.port}"
- username="${rabbitmq.username}"
- password="${rabbitmq.password}"
- virtual-host="${rabbitmq.virtual-host}"/>
-
-
"connectionFactory"/> -
-
-
"spring_queue" name="spring_queue" auto-declare="true"/> -
-
-
-
"spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/> -
-
-
"spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/> -
-
-
"spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true"> -
-
"spring_fanout_queue_1"/> -
"spring_fanout_queue_2"/> -
-
-
-
-
-
"spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/> -
-
"spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/> -
-
"spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/> -
-
"spring_topic_exchange" name="spring_topic_exchange" auto-declare="true"> -
-
"root.*" queue="spring_topic_queue_star"/> -
"root.#" queue="spring_topic_queue_well"/> -
"itcast.#" queue="spring_topic_queue_well2"/> -
-
-
-
-
"rabbitTemplate" connection-factory="connectionFactory"/>
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
- public class ProducerTest {
- //1.注入 RabbitTemplate
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- @Test
- public void testHelloWorld(){
- //2.发送消息
- rabbitTemplate.convertAndSend("spring_queue","hello world spring....");
- }
- /**
- * 发送fanout消息
- */
- @Test
- public void testFanout(){
- //2.发送消息
-
- rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout....");
- }
-
-
- /**
- * 发送topic消息
- */
- @Test
- public void testTopic(){
- //2.发送消息
-
- rabbitTemplate.convertAndSend("spring_topic_exchange","root.hehe.haha","spring topic....");
- }
- }
- "1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:rabbit="http://www.springframework.org/schema/rabbit"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/rabbit
- http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
-
-
"classpath:rabbitmq.properties"/> -
-
-
"connectionFactory" host="${rabbitmq.host}" - port="${rabbitmq.port}"
- username="${rabbitmq.username}"
- password="${rabbitmq.password}"
- virtual-host="${rabbitmq.virtual-host}"/>
-
-
"springQueueListener" class="com.rabbitmq.listener.SpringQueueListener"/> -
-
"connectionFactory" auto-declare="true"> -
-
"springQueueListener" queue-names="spring_queue"/> -
- public class SpringQueueListener implements MessageListener {
- @Override
- public void onMessage(Message message) {
- //打印消息
- System.out.println(new String(message.getBody()));
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-rabbitmq-comsumer.xml")
- public class ConsumerTest {
- @Test
- public void test1(){
- boolean flag = true;
- while (true){
-
- }
- }
-
- }