3 - [メッセージングミドルウェア - ActiveMQの中間モードのActiveMQを使用--- 3 Eメールメッセージ

メールPOP3プロトコルを開くために1163必要性

注:163件のメッセージが開いているPOP3プロトコルに必要な広告コンテンツとコンテンツを送信することはできません。

ここに画像を挿入説明

オープン認証コード:

ここに画像を挿入説明

2実装

プロデューサーの作成2.1

ここに画像を挿入説明

輸入依存度

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- spring boot web支持:mvc,aop... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 8080
spring:
  activemq:
    broker-url: tcp://192.168.153.11:61616
    user: admin
    password: admin
topic: springboot-topic

作成しTopicConfigたプロファイルを

package com.snow.config;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.jms.Topic;

@Component
public class TopicConfig {

    @Value("${topic}")
    private String topicName;
    @Bean
    public Topic topic() {
        return new ActiveMQTopic(topicName);
    }
}

作ります Producer

package com.snow.mq;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Topic;

@Component
@EnableScheduling
public class Producer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;

    @Scheduled(fixedDelay = 5000)
    public void send() {
        JSONObject jsonObject = new JSONObject();
        String userName = System.currentTimeMillis() + "";
        jsonObject.put("userName", userName);
        jsonObject.put("eamil", "[email protected]");
        String msg = jsonObject.toJSONString();
        System.out.println("生产者向消费者发送内容:" + msg);
        jmsMessagingTemplate.convertAndSend(topic, msg);
    }
}

スタートアップの項目

package com.snow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ProducerApp {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApp.class, args);
    }
}

消費者の作成2.2

ここに画像を挿入説明

輸入依存度

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- spring boot web支持:mvc,aop... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 8081
spring:
  activemq:
    broker-url: tcp://192.168.153.11:61616
    user: admin
    password: admin
  #### 开启发布订阅
  jms:
    pub-sub-domain: true
  mail:
    host: smtp.163.com
    username: [email protected]
    password: yang123456
    enable:  true
    smtp:
      auth: true
    starttls:
      enable: true
      required: true
topic: springboot-topic

作ります Consumer

package com.snow.mq;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String toEmail;

    @JmsListener(destination = "spring-topic")
    public void receive(String msg) throws Exception {
        System.out.println("消费者接受,生产者内容:" + msg);
        if (StringUtils.isEmpty(msg)) {
            return;
        }
        JSONObject jsonObject = JSONObject.parseObject(msg);
        String userName = jsonObject.getString("userName");
        String eamil = jsonObject.getString("eamil");
        sendSimpleMail(eamil, userName);

    }

    public void sendSimpleMail(String eamil, String userName) throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(eamil);
        message.setTo(toEmail);
        message.setSubject("提醒");
        message.setText("祝贺您,成为了我们" + userName + ",会员!");
        javaMailSender.send(message);
        System.out.println("邮件发送完成," + JSONObject.toJSONString(message));
    }
}

スタートアップの項目

package com.snow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class, args);
    }
}

プロジェクト、コンソールの印刷を開始します。

采用发布订阅方式,生产者向消费者发送内容:1583657784517
采用发布订阅方式,生产者向消费者发送内容:1583657789530
采用发布订阅方式,生产者向消费者发送内容:1583657794544
采用发布订阅方式,生产者向消费者发送内容:1583657799558
采用发布订阅方式,生产者向消费者发送内容:1583657804571
675元記事公開 ウォンの賞賛214 ビューに14万+を

おすすめ

転載: blog.csdn.net/weixin_42112635/article/details/104735867