|
1 | 1 | package io.kimmking.mq.rocket; |
2 | 2 |
|
| 3 | +import org.apache.rocketmq.client.producer.SendCallback; |
| 4 | +import org.apache.rocketmq.client.producer.SendResult; |
| 5 | +import org.apache.rocketmq.spring.core.RocketMQTemplate; |
| 6 | +import org.springframework.boot.CommandLineRunner; |
3 | 7 | import org.springframework.boot.SpringApplication; |
4 | 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 9 | +import org.springframework.messaging.MessageHeaders; |
| 10 | +import org.springframework.messaging.support.MessageBuilder; |
| 11 | +import org.springframework.util.MimeTypeUtils; |
| 12 | + |
| 13 | +import javax.annotation.Resource; |
| 14 | +import java.math.BigDecimal; |
| 15 | +import java.util.List; |
5 | 16 |
|
6 | 17 | @SpringBootApplication |
7 | | -public class RocketApplication { |
| 18 | +public class RocketApplication implements CommandLineRunner { |
8 | 19 |
|
9 | 20 | public static void main(String[] args) { |
10 | 21 | SpringApplication.run(RocketApplication.class, args); |
11 | 22 | } |
12 | 23 |
|
| 24 | + @Resource |
| 25 | + private RocketMQTemplate rocketMQTemplate; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void run(String... args) throws Exception { |
| 29 | + |
| 30 | + String topic = "test-k1"; |
| 31 | + SendResult sendResult = rocketMQTemplate.syncSend(topic, "Hello, World!" + System.currentTimeMillis()); |
| 32 | + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); |
| 33 | + |
| 34 | + sendResult = rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload( |
| 35 | + new Order(System.currentTimeMillis(),"CNY2USD", 0.1501d)) |
| 36 | + .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE).build()); |
| 37 | + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); |
| 38 | + |
| 39 | + String topic1 = "test-k2"; |
| 40 | + rocketMQTemplate.asyncSend(topic1, new Order(System.currentTimeMillis(),"CNY2USD", 0.1502d), new SendCallback() { |
| 41 | + @Override |
| 42 | + public void onSuccess(SendResult result) { |
| 43 | + System.out.printf("async onSucess SendResult=%s %n", result); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onException(Throwable throwable) { |
| 48 | + System.out.printf("async onException Throwable=%s %n", throwable); |
| 49 | + } |
| 50 | + |
| 51 | + }); |
| 52 | + |
| 53 | + |
| 54 | + } |
| 55 | + |
13 | 56 | } |
0 commit comments