TestRabbit.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //package me.zhengjie;
  2. //
  3. //import com.alibaba.fastjson.JSON;
  4. //import com.alibaba.fastjson.JSONArray;
  5. //import com.rabbitmq.client.*;
  6. //import lombok.SneakyThrows;
  7. //import me.zhengjie.modules.dm.daypc.domain.DmDayPc;
  8. //import me.zhengjie.modules.dm.daypc.service.DmDayPcService;
  9. //import me.zhengjie.modules.dm.food.service.DmFoodService;
  10. //import me.zhengjie.modules.dm.order.service.DmOrderRecordService;
  11. //import me.zhengjie.modules.dm.order.service.dto.DmExpenseCalendar;
  12. //import me.zhengjie.utils.ConnectionUtil;
  13. //import org.junit.Test;
  14. //
  15. //import java.io.IOException;
  16. //import java.sql.Timestamp;
  17. //import java.util.List;
  18. //import java.util.Map;
  19. //import java.util.concurrent.TimeoutException;
  20. //
  21. //public class TestRabbit {
  22. // DmFoodService dmFoodService;
  23. // DmDayPcService dmDayPcService;
  24. // DmOrderRecordService dmOrderRecordService;
  25. //
  26. // @Test
  27. // public void FoodPj(){
  28. // //获取连接
  29. // Connection connection = ConnectionUtil.getConnection();
  30. // try {
  31. // List<DmDayPc> dmDayPc = dmDayPcService.foodRepository();
  32. // String userString = JSON.toJSONString(dmDayPc);
  33. // Channel channel = connection.createChannel();
  34. // String exchangeName = "schedule_consume";//交换机
  35. // channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true,false, null);
  36. // String queue1Name = "schedule_consume";//队列名称
  37. // channel.queueDeclare(queue1Name, true, false, false, null);
  38. // channel.queueBind(queue1Name, exchangeName, "schedule_consume");// 队列绑定hello路由
  39. // channel.basicPublish(exchangeName, "schedule_consume", null, userString.getBytes());
  40. // System.out.println("sned排菜:"+dmDayPc);
  41. // channel.close();
  42. // connection.close();
  43. // } catch (IOException | TimeoutException e) {
  44. // e.printStackTrace();
  45. // }
  46. // }
  47. //
  48. // @Test
  49. // public void receive(){
  50. // Connection connection = ConnectionUtil.getConnection();
  51. // try {
  52. // Channel channel = connection.createChannel();
  53. // //通过consumer来处理数据
  54. // Consumer consumer = new DefaultConsumer(channel){
  55. // @SneakyThrows
  56. // @Override
  57. // public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  58. // //body就是从队列中获取的数据
  59. // String str = new String(body);
  60. // List<DmDayPc> list = JSONArray.parseArray(str,DmDayPc.class);
  61. // for (DmDayPc dmDayPc : list) {
  62. // dmDayPc.setPcdate(new Timestamp(System.currentTimeMillis()));
  63. // dmDayPcService.create(dmDayPc);
  64. // System.out.println("新增food==="+dmDayPc.getFood());
  65. // dmFoodService.create(dmDayPc.getFood());
  66. // }
  67. // }
  68. // };
  69. // //参数1:接收哪个队列的数据
  70. // //参数2:消息确认 是否应答,收到消息是否回复
  71. // //参数3:
  72. // channel.basicConsume("schedule_produce",true,consumer);
  73. // } catch (IOException e) {
  74. // e.printStackTrace();
  75. // }
  76. // }
  77. //
  78. // @Test
  79. // public void send(){
  80. // //获取连接
  81. // Connection connection = ConnectionUtil.getConnection();
  82. // try {
  83. // List<Map<String,Object>> map = dmOrderRecordService.selectOrderRecord();
  84. // JSONArray jsonArray = new JSONArray();
  85. // jsonArray.addAll(map);
  86. // List<DmExpenseCalendar> list = jsonArray.toJavaList(DmExpenseCalendar.class);
  87. // String userString2 = JSON.toJSONString(list);
  88. // Channel channel = connection.createChannel();
  89. // String exchangeName = "dish_trade_produce";//交换机
  90. // channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true,false, null);
  91. // String queue1Name = "dish_trade_produce";//队列名称
  92. // channel.queueDeclare(queue1Name, true, false, false, null);
  93. // channel.queueBind(queue1Name, exchangeName, "dish_trade_produce");// 队列绑定hello路由
  94. // channel.basicPublish(exchangeName, "dish_trade_produce", null, userString2.getBytes());
  95. // System.out.println("消费记录:"+userString2);
  96. //
  97. // channel.close();
  98. // connection.close();
  99. // } catch (IOException | TimeoutException e) {
  100. // e.printStackTrace();
  101. // }
  102. // }
  103. //
  104. // /**
  105. // * 消费者
  106. // */
  107. // @Test
  108. // public void receive1(){
  109. // Connection connection = ConnectionUtil.getConnection();
  110. // try {
  111. // Channel channel = connection.createChannel();
  112. // //通过consumer来处理数据
  113. // Consumer consumer = new DefaultConsumer(channel){
  114. // @SneakyThrows
  115. // @Override
  116. // public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  117. // //body就是从队列中获取的数据
  118. // String str = new String(body);
  119. // List<DmExpenseCalendar> dmOrderItem = JSONArray.parseArray(str,DmExpenseCalendar.class);
  120. // for (DmExpenseCalendar dmExpenseCalendar : dmOrderItem) {
  121. // dmOrderRecordService.createOrderItem(dmExpenseCalendar);
  122. // dmOrderRecordService.createOrderRecord(dmExpenseCalendar);
  123. // }
  124. // }
  125. // };
  126. // channel.basicConsume("dish_trade_produce",true,consumer);
  127. // } catch (IOException e) {
  128. // e.printStackTrace();
  129. // }
  130. // }
  131. //}