1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| @Service
public class MqService implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(MqService.class);
private static final String TASK_QUEUE_NAME = "simple_queue";
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(String message) throws IOException {
rabbitTemplate.convertAndSend(TASK_QUEUE_NAME, message);
logger.info("send message={}", message);
}
@Override
public void onMessage(Message message) {
String msg = null;
try {
msg = new String(message.getBody(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
logger.info("recv message=" + msg );
}
}
|