forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Dec 9, 2019
1 parent
a95edcf
commit f7bce88
Showing
6 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...-demo/src/main/java/cn/iocoder/springboot/lab04/rabbitmqdemo/consumer/Demo02Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cn.iocoder.springboot.lab04.rabbitmqdemo.consumer; | ||
|
||
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo02Message; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = Demo02Message.QUEUE) | ||
public class Demo02Consumer { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@RabbitHandler | ||
public void onMessage(Demo02Message message) { | ||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...mq-demo/src/main/java/cn/iocoder/springboot/lab04/rabbitmqdemo/message/Demo02Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cn.iocoder.springboot.lab04.rabbitmqdemo.message; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Demo02Message implements Serializable { | ||
|
||
public static final String QUEUE = "QUEUE_DEMO_02"; | ||
|
||
public static final String EXCHANGE = "EXCHANGE_DEMO_02"; | ||
|
||
public static final String ROUTING_KEY = "ROUTING_KEY_02"; | ||
|
||
/** | ||
* 编号 | ||
*/ | ||
private Integer id; | ||
|
||
public Demo02Message setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Demo02Message{" + | ||
"id=" + id + | ||
'}'; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...-demo/src/main/java/cn/iocoder/springboot/lab04/rabbitmqdemo/producer/Demo02Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cn.iocoder.springboot.lab04.rabbitmqdemo.producer; | ||
|
||
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo02Message; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Demo02Producer { | ||
|
||
@Autowired | ||
private RabbitTemplate rabbitTemplate; | ||
|
||
public void syncSend(Integer id, String routingKey) { | ||
// 创建 Demo02Message 消息 | ||
Demo02Message message = new Demo02Message(); | ||
message.setId(id); | ||
// 同步发送消息 | ||
rabbitTemplate.convertAndSend(Demo02Message.EXCHANGE, routingKey, message); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...o/src/test/java/cn/iocoder/springboot/lab04/rabbitmqdemo/producer/Demo02ProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cn.iocoder.springboot.lab04.rabbitmqdemo.producer; | ||
|
||
import cn.iocoder.springboot.lab04.rabbitmqdemo.Application; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public class Demo02ProducerTest { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private Demo02Producer producer; | ||
|
||
@Test | ||
public void testSyncSendSuccess() throws InterruptedException { | ||
int id = (int) (System.currentTimeMillis() / 1000); | ||
producer.syncSend(id, "da.yu.nai"); | ||
logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id); | ||
|
||
// 阻塞等待,保证消费 | ||
new CountDownLatch(1).await(); | ||
} | ||
|
||
@Test | ||
public void testSyncSendFailure() throws InterruptedException { | ||
int id = (int) (System.currentTimeMillis() / 1000); | ||
producer.syncSend(id, "yu.nai.shuai"); | ||
logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id); | ||
|
||
// 阻塞等待,保证消费 | ||
new CountDownLatch(1).await(); | ||
} | ||
|
||
|
||
} |