zxy il y a 2 ans
Parent
commit
422321c93c

+ 18 - 5
pom.xml

@@ -25,6 +25,10 @@
25 25
             <groupId>org.springframework.boot</groupId>
26 26
             <artifactId>spring-boot-starter-data-jpa</artifactId>
27 27
         </dependency>
28
+        <dependency>
29
+            <groupId>org.springframework.boot</groupId>
30
+            <artifactId>spring-boot-starter-amqp</artifactId>
31
+        </dependency>
28 32
         <dependency>
29 33
             <groupId>org.projectlombok</groupId>
30 34
             <artifactId>lombok</artifactId>
@@ -54,11 +58,20 @@
54 58
             <artifactId>hutool-all</artifactId>
55 59
             <version>4.1.1</version>
56 60
         </dependency>
57
-<!--        <dependency>-->
58
-<!--            <groupId></groupId>-->
59
-<!--            <artifactId></artifactId>-->
60
-<!--        </dependency>-->
61
-
61
+        <dependency>
62
+            <groupId>com.sun.mail</groupId>
63
+            <artifactId>javax.mail</artifactId>
64
+            <version>1.6.2</version>
65
+        </dependency>
66
+        <dependency>
67
+            <groupId>junit</groupId>
68
+            <artifactId>junit</artifactId>
69
+        </dependency>
70
+        <dependency>
71
+            <groupId>com.alibaba</groupId>
72
+            <artifactId>fastjson</artifactId>
73
+            <version>1.2.72</version>
74
+        </dependency>
62 75
     </dependencies>
63 76
 
64 77
     <build>

+ 1 - 1
src/main/java/com/example/springbootdemo/SpringbootdemoApplication.java

@@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
6 6
 import org.springframework.context.ConfigurableApplicationContext;
7 7
 import org.springframework.context.annotation.ComponentScan;
8 8
 @SpringBootApplication(scanBasePackages = {"com.example"})
9
-@ComponentScan({"com.example.demo","com.example.springbootdemo"})
9
+@ComponentScan({"com.example"})
10 10
 public class SpringbootdemoApplication {
11 11
     public static void main(String[] args) {
12 12
         ConfigurableApplicationContext run  = SpringApplication.run(SpringbootdemoApplication.class, args);

+ 31 - 0
src/main/java/com/example/springbootdemo/config/AsyncConfiguration.java

@@ -0,0 +1,31 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+import org.springframework.scheduling.annotation.EnableAsync;
6
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
7
+
8
+import java.util.concurrent.Executor;
9
+import java.util.concurrent.ThreadPoolExecutor;
10
+
11
+@Configuration
12
+@EnableAsync
13
+public class AsyncConfiguration {
14
+    @Bean("doSomethingExecutor")
15
+    public Executor doExecutor() {
16
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
17
+        executor.setCorePoolSize(10);
18
+        // 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
19
+        executor.setMaxPoolSize(20);
20
+        // 缓冲队列:用来缓冲执行任务的队列
21
+        executor.setQueueCapacity(500);
22
+        // 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
23
+        executor.setKeepAliveSeconds(60);
24
+        // 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
25
+        executor.setThreadNamePrefix("do-something-");
26
+        // 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
27
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
28
+        executor.initialize();
29
+        return executor;
30
+    }
31
+}

+ 25 - 0
src/main/java/com/example/springbootdemo/config/CustomRabbitListener.java

@@ -0,0 +1,25 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import com.rabbitmq.client.Channel;
4
+import org.springframework.amqp.core.Message;
5
+import org.springframework.amqp.rabbit.annotation.Exchange;
6
+import org.springframework.amqp.rabbit.annotation.Queue;
7
+import org.springframework.amqp.rabbit.annotation.QueueBinding;
8
+import org.springframework.messaging.handler.annotation.Headers;
9
+import org.springframework.stereotype.Component;
10
+
11
+import java.util.Map;
12
+
13
+@Component
14
+public class CustomRabbitListener {
15
+//    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "crm_foot_info_14049")
16
+//    public void a(Message message) {
17
+//        System.out.println(message);
18
+//    }
19
+//    @org.springframework.amqp.rabbit.annotation.RabbitListener(
20
+//            queues = RabbitMQConfiguration.dealQueueOrder
21
+//    )
22
+//    public void process(String order, Message message, @Headers Map<String, Object> headers, Channel channel) {
23
+//        System.out.println(order);
24
+//    }
25
+}

+ 12 - 0
src/main/java/com/example/springbootdemo/config/MyBeanConfig.java

@@ -0,0 +1,12 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+
6
+@Configuration
7
+public class MyBeanConfig {
8
+    @Bean
9
+    public String getName() {
10
+        return "nimadeo";
11
+    }
12
+}

+ 87 - 0
src/main/java/com/example/springbootdemo/config/RabbitMQConfiguration.java

@@ -0,0 +1,87 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import org.springframework.amqp.core.*;
4
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.annotation.Bean;
7
+import org.springframework.context.annotation.Configuration;
8
+
9
+import java.util.HashMap;
10
+import java.util.Map;
11
+
12
+@Configuration
13
+public class RabbitMQConfiguration {
14
+    //队列名称
15
+    public   final static String orderQueue = "order_queue";
16
+
17
+    //交换机名称
18
+    public  final static String orderExchange = "order_exchange";
19
+
20
+    // routingKey
21
+    public  final static String routingKeyOrder = "routing_key_order";
22
+
23
+    //死信消息队列名称
24
+    public  final static String dealQueueOrder = "deal_queue_order";
25
+
26
+    //死信交换机名称
27
+    public  final static String dealExchangeOrder = "deal_exchange_order";
28
+
29
+    //死信 routingKey
30
+    public final static String deadRoutingKeyOrder = "dead_routing_key_order";
31
+
32
+    //死信队列 交换机标识符
33
+    public static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";
34
+
35
+    //死信队列交换机绑定键标识符
36
+    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";
37
+
38
+    @Autowired
39
+    private CachingConnectionFactory connectionFactory;
40
+
41
+    @Bean
42
+    public Queue orderQueue() {
43
+        // 将普通队列绑定到死信队列交换机上
44
+        Map<String, Object> args = new HashMap<>(2);
45
+        //args.put("x-message-ttl", 5 * 1000);//直接设置 Queue 延迟时间 但如果直接给队列设置过期时间,这种做法不是很灵活
46
+        //这里采用发送消息动态设置延迟时间,这样我们可以灵活控制
47
+        args.put(DEAD_LETTER_QUEUE_KEY, dealExchangeOrder);
48
+        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKeyOrder);
49
+        return new Queue(RabbitMQConfiguration.orderQueue, true, false, false, args);
50
+    }
51
+
52
+    //声明一个direct类型的交换机
53
+    @Bean
54
+    DirectExchange orderExchange() {
55
+        return new DirectExchange(RabbitMQConfiguration.orderExchange);
56
+    }
57
+    @Bean
58
+    FanoutExchange fanoutT1Exchange() {
59
+        return new FanoutExchange("TT1");
60
+    }
61
+
62
+    //绑定Queue队列到交换机,并且指定routingKey
63
+    @Bean
64
+    Binding bindingDirectExchangeDemo5(   ) {
65
+        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(routingKeyOrder);
66
+    }
67
+
68
+    //创建配置死信队列
69
+    @Bean
70
+    public Queue deadQueueOrder() {
71
+        Queue queue = new Queue(dealQueueOrder, true);
72
+        return queue;
73
+    }
74
+
75
+    //创建死信交换机
76
+    @Bean
77
+    public DirectExchange deadExchangeOrder() {
78
+        return new DirectExchange(dealExchangeOrder);
79
+    }
80
+
81
+    //死信队列与死信交换机绑定
82
+    @Bean
83
+    public Binding bindingDeadExchange() {
84
+        return BindingBuilder.bind(deadQueueOrder()).to(deadExchangeOrder()).with(deadRoutingKeyOrder);
85
+    }
86
+
87
+}

+ 47 - 1
src/main/java/com/example/springbootdemo/controller/HelloController.java

@@ -4,19 +4,30 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
4 4
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5 5
 import com.example.springbootdemo.entity.FootScanEntity;
6 6
 import com.example.springbootdemo.entity.FootScanGroup;
7
+import com.example.springbootdemo.form.PostForm;
7 8
 import com.example.springbootdemo.mapper.FootScanMapper;
9
+import com.example.springbootdemo.query.DateQuery;
8 10
 import com.example.springbootdemo.service.FootScanGroupService;
9 11
 import com.example.springbootdemo.service.FootScanService;
10 12
 import com.example.springbootdemo.zxyTeST.sql.dao.ArmGrabRepository;
11 13
 import com.example.springbootdemo.zxyTeST.sql.dao.ColorRepository;
12 14
 import com.example.springbootdemo.zxyTeST.sql.entity.ArmGrabEntity;
13 15
 import com.example.springbootdemo.zxyTeST.sql.entity.Color;
16
+import com.example.util.MailUtil;
14 17
 import org.springframework.beans.factory.annotation.Autowired;
15 18
 import org.springframework.dao.EmptyResultDataAccessException;
16 19
 import org.springframework.jdbc.core.JdbcTemplate;
17 20
 import org.springframework.web.bind.annotation.*;
18 21
 
22
+import javax.mail.Address;
23
+import javax.mail.Session;
24
+import javax.mail.Transport;
25
+import javax.mail.internet.MimeMessage;
26
+import java.text.SimpleDateFormat;
19 27
 import java.util.List;
28
+import java.util.Properties;
29
+import java.util.concurrent.ConcurrentHashMap;
30
+import java.util.logging.SimpleFormatter;
20 31
 
21 32
 @RestController
22 33
 public class HelloController {
@@ -32,7 +43,8 @@ public class HelloController {
32 43
     FootScanGroupService footScanGroupService;
33 44
     @Autowired
34 45
     FootScanMapper footScanMapper;
35
-
46
+    @Autowired
47
+    MailUtil mailUtil;
36 48
 
37 49
     @RequestMapping("/hello")
38 50
     public void hello() {
@@ -81,6 +93,40 @@ public class HelloController {
81 93
 //        footScanService.page(footScanEntityPage,null);
82 94
 //        return  footScanEntityPage.getRecords();
83 95
 //    }
96
+    @RequestMapping("testDate")
97
+    public String testDate(DateQuery dateQuery) {
98
+        SimpleDateFormat simpleFormatter = new SimpleDateFormat("今天是 yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
99
+        return simpleFormatter.format(dateQuery.getDate());
100
+    }
101
+    @PostMapping("postTest")
102
+    public void testPost(@RequestBody PostForm postForm) {
103
+        ConcurrentHashMap<String, String> stringStringConcurrentHashMap = new ConcurrentHashMap<>();
104
+        stringStringConcurrentHashMap.put("1","123");
105
+        System.out.println(postForm.getName());
106
+    }
107
+
108
+    @RequestMapping("testMail")
109
+    public void testMail() {
110
+        Properties properties = new Properties();
111
+        properties.setProperty("mail.transport.protocol", "smtp");   // 使用的协议(JavaMail规范要求)
112
+        properties.setProperty("mail.smtp.host", "smtp.exmail.qq.com");   // 发件人的邮箱的 SMTP 服务器地址
113
+        properties.setProperty("mail.smtp.auth", "true");
114
+        Session session = Session.getDefaultInstance(properties);
115
+        session.setDebug(true);                                 // 设置为debug模式, 可以查看详细的发送 log
116
+        try {
117
+            MimeMessage message = mailUtil.createMimeMessage(session, "software@semsx.com",
118
+                    "771799477@qq.com,1152047207@qq.com");
119
+            Transport transport = session.getTransport();
120
+            transport.connect("software@semsx.com","RNDmdjNarRkqg4ge");
121
+            Address[] allRecipients = message.getAllRecipients();
122
+        //    transport.sendMessage(message,message.getAllRecipients());
123
+            transport.close();
124
+        } catch (Exception e) {
125
+            e.printStackTrace();
126
+        }
127
+
128
+    }
129
+
84 130
 
85 131
 }
86 132
 

+ 1 - 0
src/main/java/com/example/springbootdemo/controller/MybatisPlusController.java

@@ -7,6 +7,7 @@ import com.example.springbootdemo.entity.FootScanEntity;
7 7
 import com.example.springbootdemo.mapper.DeviceMapper;
8 8
 import com.example.springbootdemo.service.FootScanService;
9 9
 import com.example.springbootdemo.service.GoodsBindsShoesLastService;
10
+import org.apache.tomcat.util.codec.binary.StringUtils;
10 11
 import org.springframework.beans.factory.annotation.Autowired;
11 12
 import org.springframework.web.bind.annotation.RequestMapping;
12 13
 import org.springframework.web.bind.annotation.RequestParam;

+ 16 - 0
src/main/java/com/example/springbootdemo/controller/TestBeanController.java

@@ -0,0 +1,16 @@
1
+package com.example.springbootdemo.controller;
2
+
3
+import com.example.springbootdemo.config.MyBeanConfig;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.RestController;
7
+
8
+@RestController
9
+public class TestBeanController {
10
+    @Autowired
11
+    MyBeanConfig myBeanConfig;
12
+    @RequestMapping("/mybean")
13
+    public void myBean() {
14
+        System.out.println(myBeanConfig.getName());
15
+    }
16
+}

+ 24 - 0
src/main/java/com/example/springbootdemo/controller/TrainJavaController.java

@@ -1,13 +1,18 @@
1 1
 package com.example.springbootdemo.controller;
2 2
 
3
+import com.example.springbootdemo.config.RabbitMQConfiguration;
3 4
 import com.example.springbootdemo.zxyTeST.sql.dao.FootScanCommonRepository;
4 5
 import com.example.springbootdemo.zxyTeST.sql.entity.FootScanCommonEntity;
5 6
 import com.example.springbootdemo.zxyTeST.ymlConfig.SemsxUser;
7
+import com.rabbitmq.client.Channel;
8
+import org.springframework.amqp.core.Message;
9
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
6 10
 import org.springframework.beans.factory.annotation.Autowired;
7 11
 import org.springframework.web.bind.annotation.*;
8 12
 
9 13
 import java.util.List;
10 14
 import java.util.Map;
15
+import java.util.UUID;
11 16
 
12 17
 @RestController
13 18
 public class TrainJavaController {
@@ -15,6 +20,8 @@ public class TrainJavaController {
15 20
     FootScanCommonRepository footScanCommonRepository;
16 21
     @Autowired
17 22
     SemsxUser semsxUser;
23
+    @Autowired
24
+    RabbitTemplate rabbitTemplate;
18 25
     @RequestMapping("/footScan")
19 26
     public String getFootScanInfo() {
20 27
         FootScanCommonEntity  footScanCommonInfo = footScanCommonRepository.findByFootScanId(14815);
@@ -33,4 +40,21 @@ public class TrainJavaController {
33 40
         return  semsxUser;
34 41
     }
35 42
 
43
+    @RequestMapping("/dtl")
44
+    public String dtl(Message message1, Channel channel) {
45
+        String orderId = UUID.randomUUID().toString();
46
+        rabbitTemplate.convertAndSend(
47
+                RabbitMQConfiguration.orderExchange, //发送至订单交换机
48
+                RabbitMQConfiguration.routingKeyOrder, //订单定routingKey
49
+                orderId //订单号   这里可以传对象 比如直接传订单对象
50
+                , message -> {
51
+                    // 如果配置了 params.put("x-message-ttl", 5 * 1000);
52
+                    // 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
53
+                    message.getMessageProperties().setExpiration(5 * 10 + "");
54
+                    String msg = new String(message.getBody());
55
+                    return message;
56
+                });
57
+
58
+        return "{'orderId':'"+orderId+"'}";
59
+    }
36 60
 }

+ 17 - 0
src/main/java/com/example/springbootdemo/entity/Person.java

@@ -0,0 +1,17 @@
1
+package com.example.springbootdemo.entity;
2
+
3
+import com.alibaba.fastjson.annotation.JSONField;
4
+import lombok.Data;
5
+
6
+@Data
7
+public class Person {
8
+    @JSONField(name = "aage")
9
+    private int age;
10
+    @JSONField(name = "nname")
11
+    private String name;
12
+
13
+    public Person(int age, String name) {
14
+        this.age = age;
15
+        this.name = name;
16
+    }
17
+}

+ 8 - 0
src/main/java/com/example/springbootdemo/form/PostForm.java

@@ -0,0 +1,8 @@
1
+package com.example.springbootdemo.form;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class PostForm {
7
+    private String name;
8
+}

+ 12 - 0
src/main/java/com/example/springbootdemo/query/DateQuery.java

@@ -0,0 +1,12 @@
1
+package com.example.springbootdemo.query;
2
+
3
+import lombok.Data;
4
+import org.springframework.format.annotation.DateTimeFormat;
5
+
6
+import java.util.Date;
7
+
8
+@Data
9
+public class DateQuery {
10
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
11
+    private Date date;
12
+}

+ 34 - 0
src/main/java/com/example/util/MailUtil.java

@@ -0,0 +1,34 @@
1
+package com.example.util;
2
+
3
+import com.example.springbootdemo.config.MailConfig;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.stereotype.Component;
6
+
7
+import javax.mail.Session;
8
+import javax.mail.internet.InternetAddress;
9
+import javax.mail.internet.MimeMessage;
10
+import java.util.Date;
11
+@Component
12
+public class MailUtil {
13
+    @Autowired
14
+    MailConfig mailConfig;
15
+    public MimeMessage createMimeMessage(Session session, String sendMail, String users) throws Exception {
16
+        // 1. 创建一封邮件
17
+        MimeMessage message = new MimeMessage(session);
18
+        // 2. From: 发件人
19
+        message.setFrom(new InternetAddress(sendMail, "【semsx】", "UTF-8"));
20
+        // 3. To: 收件人(可以增加多个收件人、抄送、密送)
21
+//        Address[] internetAddresses = new InternetAddress().parse(users);
22
+        message.setRecipients(MimeMessage.RecipientType.TO, users);
23
+        // 4. Subject: 邮件主题
24
+        message.setSubject("打折钜惠", "UTF-8");
25
+        // 5. Content: 邮件正文(可以使用html标签)
26
+        message.setContent("XX用户你好 <br/> 今天全场5折,<br/> 快来抢购, 错过今天再等一年。。。", "text/html;charset=UTF-8");
27
+        // 6. 设置发件时间
28
+        message.setSentDate(new Date());
29
+        // 7. 保存设置
30
+        message.saveChanges();
31
+        return message;
32
+    }
33
+
34
+}

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

@@ -8,7 +8,7 @@ semsx :
8 8
     - 5
9 9
     - 6
10 10
 server:
11
-  port: 8082
11
+  port: 7041
12 12
 spring:
13 13
   datasource:
14 14
     druid:
@@ -20,7 +20,7 @@ spring:
20 20
       driver-class-name: com.mysql.cj.jdbc.Driver
21 21
       url: jdbc:mysql://192.168.0.201:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
22 22
       username: root
23
-      password:
23
+      password: 123456
24 24
   jpa:
25 25
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
26 26
     open-in-view: false

+ 8 - 1
src/main/resources/application.yml

@@ -11,13 +11,20 @@ server:
11 11
   port: 8081
12 12
 spring:
13 13
   datasource:
14
-    url: jdbc:mysql://192.168.2.130:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
14
+    url: jdbc:mysql://192.168.0.201:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
15 15
     username: root
16 16
     password: 123456
17 17
     driver-class-name: com.mysql.cj.jdbc.Driver
18 18
   jpa:
19 19
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
20 20
     open-in-view: false
21
+  rabbitmq:
22
+    host: 192.168.0.100
23
+    port: 5672
24
+    username: guest
25
+    password: guest
26
+    publisher-returns: true
27
+    virtual-host: /scanmq
21 28
 mybatis-plus:
22 29
   configuration:
23 30
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Fichier diff supprimé car celui-ci est trop grand
+ 17 - 0
src/test/java/com/example/springbootdemo/SpringbootdemoApplicationTests.java