어떻게 ActiveMQ 미들웨어 방식 으로 메 일 을 보 냅 니까?

머리말
대형 인터넷 회사 에서 보통 내부 의 정보 서비스 플랫폼 을 구축 하고 회사 내부 의 각종 정보 서 비 스 를 처리한다.예 를 들 어 메 일 발송,문자 발송,위 챗 푸 시 등 이다.회사 내부 정보 플랫폼 은 세 번 째 메시지 플랫폼 을 호출 한다.정보 서비스 플랫폼 의 인 터 페 이 스 는 두 가지 가 있 습 니 다.동기 화 와 비동기 화 수 요 는 보통 제3자 플랫폼 을 호출 하 는 데 성공 하 는 지,그렇지 않 으 면 모두 비동기 화 를 사용 해 야 합 니 다.
사내 정보 서비스 플랫폼

내부 정보 서비스 플랫폼 을 호출 하고 메시지 미들웨어 를 사용 합 니 다.
메 시 지 를 보 낼 때 보통 메시지 템 플 릿 을 만 들 고 호출 할 때 템 플 릿 id 를 전달 합 니 다.

Eamil 을 통합 하여 비동기 메 일 메 시 지 를 보 냅 니 다.
163 메 일 인 터 페 이 스 를 사용 하여 163 메 일 을 먼저 등록 하고 pop 3 협 의 를 개통 합 니 다.이때 권한 수여 코드 가 있 습 니 다.발송 내용 에 광 고 를 가지 고 있 으 면 안 됩 니 다.그렇지 않 으 면 163 에 의 해 차단 되 어 발송 에 실 패 했 습 니 다.
장면:회원 서비스 가 내부 정보 서비스 플랫폼 으로 메 일 을 보 냅 니 다.
회원 서비스(생산자)
Maven 의존

        <!-- SpringBoot  web   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- ActiveMQ   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!-- FastJson    -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>LATEST</version>
        </dependency>
YML 프로필

spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616    # ActiveMQ            
    user: admin  #    
    password: admin  #  
queue: springboot-queue   #      ,        
server:
  port: 8080
설정 클래스

@Configuration
public class QueueConfig {
    //     
    @Value("${queue}")
    private String queue;
    //  bean
    @Bean
    public Queue logQueue() {
        return new ActiveMQQueue(queue);
    }
}
프로듀서 만 들 기

@Component
@EnableScheduling
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;
    //  5          
    @Scheduled(fixedDelay = 5000)
    public void send(){
        //   ,         
        String userName = "  "+ new Random().nextInt(20);
        //          (       )
        String email = "593****[email protected]";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userName",userName);
        jsonObject.put("email",email);
        //         Json   
        String msg = jsonObject.toJSONString();
        System.out.println("           :" +  msg);
        jmsMessagingTemplate.convertAndSend(queue,msg);
    }
}
시작 클래스

@EnableScheduling
@SpringBootApplication
public class ActivemqQueueApplication {
    public static void main(String[] args) {
        SpringApplication.run(ActivemqQueueApplication.class, args);
    }
}
정보 서비스 플랫폼(소비자)
Maven 의존

        <!-- SpringBoot  Web   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
       <!-- activemq   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!-- SpringBoot   Emai -->
        <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>LATEST</version>
        </dependency>
YML 프로필

spring:
  activemq:
    # ActiveMQ            
    broker-url: tcp://127.0.0.1:61616
    user: admin  #    
    password: admin  #  
  #       
  mail:
    #        
    host: smtp.163.com
    #      (     )
    username: *********@163.com
    #    ,163               ,      
    password: wx1**********3
    enable:  true
    smtp:
      auth: true
    starttls:
      enable: true
      required: true
#        ,        (          )
queue: springboot-queue
server:
  port: 8081
소비자 만 들 기

@Component
public class Consumer{
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}") //     
    private String toEmail;
    @JmsListener(destination = "${queue}")     //     ,      queue  
    public void receive(String msg){
        if(StringUtils.isEmpty(msg)){
            return;
        }
        System.out.println("       :"+msg);
        JSONObject jsonObject = JSONObject.parseObject(msg);
        String userName = jsonObject.getString("userName");
        String eamil = jsonObject.getString("eamil");
        //    
        sendEmail(userName,toEmail);
    }
    /**
     *       
     * @param userName     ,        
     * @param toEmail         
     * @return
     */
    public String sendEmail(String userName,String toEmail){
        //      
        SimpleMailMessage message = new SimpleMailMessage();
        //   
        message.setFrom(toEmail);
        //   
        message.setTo(toEmail);
        //  (  )
        message.setSubject("   "+userName);
        //  
        message.setText("             !");  //  html,  html    
        javaMailSender.send(message);
        System.out.println("      :"+ JSONObject.toJSONString(message));
        return  "send success!";
    }
}
시작 클래스

@SpringBootApplication
public class ActivemqQueueConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ActivemqQueueConsumerApplication.class, args);
    }
}
테스트
회원 서비스 와 정보 서비스 플랫폼 서 비 스 를 시작 합 니 다4.567917.주의:시작 하기 전에 메시지 대기 열 에 메시지 가 없 음 을 보증 합 니 다.있 으 면 이 대기 열 을 삭제 합 니 다.그렇지 않 으 면 데이터 형식 이 일치 하지 않 아 JSon 분석 에 이상 이 발생 할 수 있 습 니 다회원 서비스:
在这里插入图片描述
메시지 서비스 플랫폼:

이 때 메시지 서비스 플랫폼 에서 메 일 발송 성공!
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기