Pārlūkot izejas kodu

初始化提交

hbjzws 1 gadu atpakaļ
vecāks
revīzija
cc8d6e95fe

+ 21 - 0
pom.xml

@@ -241,6 +241,27 @@
             <artifactId>easypoi-annotation</artifactId>
             <version>4.4.0</version>
         </dependency>
+        <!--rabbmitmq-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-amqp</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpmime</artifactId>
+            <version>4.5.10</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.data</groupId>
+            <artifactId>spring-data-commons</artifactId>
+            <version>2.2.1.RELEASE</version>
+        </dependency>
     </dependencies>
 
 

+ 2 - 0
src/main/java/com/example/demo1/Demo1Application.java

@@ -2,6 +2,7 @@ package com.example.demo1;
 
 
 import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.amqp.rabbit.annotation.EnableRabbit;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.scheduling.annotation.EnableAsync;
@@ -12,6 +13,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
 @EnableTransactionManagement
 @EnableAsync(proxyTargetClass=true)
 @MapperScan("com.example.demo1.mapper,com.example.demo1.sys.dao")
+@EnableRabbit
 @EnableScheduling
 public class Demo1Application {
 

+ 47 - 5
src/main/java/com/example/demo1/TestController.java

@@ -1,5 +1,7 @@
 package com.example.demo1;
 
+import cn.hutool.core.lang.UUID;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.ValueOperations;
@@ -19,20 +21,60 @@ public class TestController {
 
     @Autowired
     private ValueOperations<String, Object> valueOperations;
-
+    @Autowired
+    RabbitTemplate rabbitTemplate;
 
     @RequestMapping("/getName")
     @GetMapping
     public String getUsernam(@RequestAttribute String subject)
     {
 
-        valueOperations.setIfAbsent("ttt_89871" , true, 20, TimeUnit.MINUTES);
-        valueOperations.set("code_09121", new Date(), 30000, TimeUnit.SECONDS);
-        Date dateCurr= (Date)valueOperations.get("code_09121");
-        System.out.println(dateCurr);
+//        valueOperations.setIfAbsent("ttt_89871" , true, 20, TimeUnit.MINUTES);
+//        valueOperations.set("code_09121", new Date(), 30000, TimeUnit.SECONDS);
+//        Date dateCurr= (Date)valueOperations.get("code_09121");
+        String id = UUID.fastUUID().toString();
+        System.out.println("--id:--"+id);
+        rabbitTemplate.convertAndSend("exchange_test1", "test1",
+                id);
+//        rabbitTemplate.convertAndSend("delayExchange", "test123",
+//                UUID.fastUUID().toString(), (message) -> {
+//                    message.getMessageProperties().setHeader("x-delay", 5*60*1000);//5分钟
+//                    //message.getMessageProperties().setHeader("x-delay", 60*1000);//1分钟测试用
+//                    return message;
+//                });
+      //  System.out.println(dateCurr);
         System.out.println("111");
         System.out.println("subject:"+subject);
         return "111";
     }
+    @RequestMapping("/getName1")
+    @GetMapping
+    public String getUsernam1(@RequestAttribute String subject)
+    {
+
+        String id = UUID.fastUUID().toString();
+        System.out.println("--id:--"+id);
+        rabbitTemplate.convertAndSend( "test5",
+                id);
+
+        System.out.println("111");
+
+        return "111";
+    }
+    @RequestMapping("/getName2")
+    @GetMapping
+    public String getUsernam2(@RequestAttribute String subject)
+    {
+        String id = UUID.fastUUID().toString();
+        System.out.println("--id:--"+id);
 
+        rabbitTemplate.convertAndSend("delayExchangeTest", "test3",
+                UUID.fastUUID().toString(), (message) -> {
+                    message.getMessageProperties().setHeader("x-delay", 2*60*1000);//1分钟测试用
+                    return message;
+                });
+        System.out.println("111");
+        System.out.println("subject:"+subject);
+        return "111";
+    }
 }

+ 84 - 0
src/main/java/com/example/demo1/config/RabbitmqConfig.java

@@ -0,0 +1,84 @@
+package com.example.demo1.config;
+
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.CustomExchange;
+import org.springframework.amqp.core.Queue;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Configuration
+public class RabbitmqConfig {
+
+
+
+    @Bean
+    public CustomExchange delayExchange() {
+        Map<String, Object> args = new HashMap<>();
+        args.put("x-delayed-type", "direct");
+
+        return new CustomExchange("delayExchangeTest", "x-delayed-message",true, false,args);
+    }
+
+//    @Bean
+//    public Queue appointmentConfirmCountDownQueue() {
+//        Queue queue = new Queue("appointmentConfirmCountDownQueue", true);
+//        return queue;
+//    }
+
+    /**
+     * 车主到达充电倒计时
+     * @return
+     */
+//    @Bean
+//    public Queue arriveChargingCountDownQueue() {
+//        Queue queue = new Queue("arriveChargingCountDownQueue", true);
+//        return queue;
+//    }
+//
+//    /**
+//     * 预约确认倒计时
+//     * @return
+//     */
+//    @Bean
+//    public Binding appointmentConfirmCountBinding() {
+//        return BindingBuilder.bind(appointmentConfirmCountDownQueue())
+//                .to(delayExchange())
+//                .with("appointmentConfirmCountDownSubmit").noargs();
+//    }
+//
+//    /**
+//     * 车主到达充电倒计时
+//     * @return
+//     */
+//    @Bean
+//    public Binding arriveChargingCountDownBinding() {
+//        return BindingBuilder.bind(arriveChargingCountDownQueue())
+//                .to(delayExchange())
+//                .with("arriveChargingCountDownSubmit").noargs();
+//    }
+//
+//    /**
+//     * 站点延时调价队列
+//     * @return
+//     */
+//    @Bean
+//    public Queue stationPriceScheduleQueue() {
+//        Queue queue = new Queue("stationPriceScheduleQueue", true);
+//        return queue;
+//    }
+//
+//    /**
+//     * 站点延时调价
+//     * @return
+//     */
+//    @Bean
+//    public Binding stationPriceScheduleBinding() {
+//        return BindingBuilder.bind(stationPriceScheduleQueue())
+//                .to(delayExchange())
+//                .with("stationPriceScheduleSubmit").noargs();
+//    }
+}

+ 22 - 0
src/main/java/com/example/demo1/mq/listener/Test3ResultListener.java

@@ -0,0 +1,22 @@
+package com.example.demo1.mq.listener;
+
+import cn.hutool.core.date.DateTime;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+@Slf4j
+@Component
+@RabbitListener(queues = "test3",concurrency = "3")
+public class Test3ResultListener {
+    @RabbitHandler
+    public void process(String id) {
+
+
+        System.out.println("333333333333:"+id+"---"+DateTime.now());
+    }
+
+}

+ 18 - 0
src/main/java/com/example/demo1/mq/listener/Test5ResultListener.java

@@ -0,0 +1,18 @@
+package com.example.demo1.mq.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+@RabbitListener(queues = "test5",concurrency = "3")
+public class Test5ResultListener {
+    @RabbitHandler
+    public void process(String id) {
+
+        System.out.println("5555555555555:"+id);
+    }
+
+}

+ 18 - 0
src/main/java/com/example/demo1/mq/listener/TestResultListener.java

@@ -0,0 +1,18 @@
+package com.example.demo1.mq.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+@RabbitListener(queues = "test1",concurrency = "3")
+public class TestResultListener {
+    @RabbitHandler
+    public void process(String id) {
+
+        System.out.println("111111111:"+id);
+    }
+
+}

+ 0 - 2
src/main/resources/application-dev.yml

@@ -12,8 +12,6 @@ spring:
     database: 3
     # Redis服务器地址
     host: 127.0.0.1
-  rabbitmq:
-    virtual-host: housekeeper-dev
 logger:
   level: INFO
   dir: "D:\\logs\\demo1"

+ 4 - 4
src/main/resources/application.yml

@@ -104,12 +104,12 @@ spring:
       timeout: 0
   rabbitmq:
     #    host: 119.36.146.59
-    host: 192.168.33.20
+    host: 127.0.0.1
     port: 5672
-    username: admin
-    password: admin
+    username: ws
+    password: ws
     #虚拟host 可以不设置,使用server默认host
-    virtual-host: devmq
+    virtual-host: jzlg-dev
   thymeleaf:
     cache: false
     mode: HTML