Set up monitoring based on the observer mode|| and use the listener to monitor the implementation, handle multi-channel message notifications (SMS + mailbox + official account message push, etc.)

Observer description

1. The basic concept of the observer mode

When an object's state changes, it is notified to all other objects

2. Application scenarios of the observer mode

Zk event monitoring, distributed configuration center refreshing configuration files, and mass sending of messages from different channels in the business

Insert picture description here

1. Use observer mode to set up monitoring

Using the observer mode is an asynchronous execution of multi-channel message notifications (SMS + mailbox + official account message push, etc.)

1. SpringUtils tool class

/**
 * SpringUtils 工具类
 */
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }

    public static <T> T getBean(Class<T> requiredType) {
        return (T) applicationContext.getBean(requiredType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }
}

2. Create an observer interface

/**
 * 观察者接口
 */
public interface ObServer {

    void sendMsg(JSONObject jsonObject);
}

3. Create Observer One

@Component
@Slf4j
public class EmailObServer implements ObServer {
    @Override
    public void sendMsg(JSONObject jsonObject) {
        log.info("使用观察者发送邮件");
    }
}

4. Create Observer Two

@Slf4j
@Component
public class SMSObServer implements ObServer { @Override public void sendMsg(JSONObject jsonObject) {log.info ("Use observer to send SMS"); } }




5. All, observer container


import com.alibaba.fastjson.JSONObject;
import com.xijia.xj.observer.ObServer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 观察者容器, listObServer = 所有的观察类
 * @author wangsong
 * @mail [email protected]
 * @date 2020/9/2 0002 21:22
 * @version 1.0.0
 */
@Component
public class XjSubject {

    /**
     * 类型 ObServer
     */
    private List<ObServer> listObServer = new ArrayList<>();
    /**
     * 线程池
     */
    private ExecutorService executorService;


    public XjSubject() {
        executorService = Executors.newFixedThreadPool(10);
    }

    /**
     * 新增ObServer
     *
     * @param obServer
     */
    public void addObServer(ObServer obServer) {
        listObServer.add(obServer);
    }

    /**
     * 通知给所有的观察者,遍历执行所有观察者的方法
     *
     * @param jsonObject
     */
    public void notifyObServer(JSONObject jsonObject) {
        for (ObServer obServer : listObServer) {
            // 单独开启线程异步通知
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    obServer.sendMsg(jsonObject);
                }
            });
        }
    }
}

6. Automatically register all observers to the observer container

import com.xijia.xj.config.SpringUtils;
import com.xijia.xj.observer.ObServer;
import com.xijia.xj.observer.subject.XjSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Map;
/**
 * 项目启动后需要 执行的一些方法 -->  注册观察者, 当我们的SpringBoot启动成功的时候,注册我们的SMSObServer
 * @author wangsong
 * @mail [email protected]
 * @date 2020/9/2 0002 21:27
 * @version 1.0.0
 */
@Component
public class InitService implements ApplicationRunner {


    @Autowired
    private XjSubject xjSubject;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        /**
         * 自动注册我们观察者
         * 1.使用Spring获取该ObServer下有那些bean对象
         * 2.直接注添加到集合中
         */
        //根据接口类型返回相应的所有bean
        Map<String, ObServer> map = SpringUtils.getApplicationContext().getBeansOfType(ObServer.class);
        for (String key : map.keySet()) {
            ObServer obServer = map.get(key);
            xjSubject.addObServer(obServer);
        }
    }
}

7. Use observer mode

@RestController
@Slf4j
public class OrderService {
   
    @Autowired
    private XjSubject xjSubject;

    @RequestMapping("/addOrder")
    public String addOrder() {
        log.info("1.调用数据库下单订单代码:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sms", "1865891111");
        jsonObject.put("email", "[email protected]");
        xjSubject.notifyObServer(jsonObject);
        return "success";
    }
  }

8. Execution effect

Insert picture description here

Second, use spring's Listener to monitor

Note that spring's Listener is executed synchronously and does not support @Async asynchronous annotation

1. Create a parameter class for monitoring delivery

package com.xijia.spring.entity;

import org.springframework.context.ApplicationEvent;

public class UserMessageEntity extends ApplicationEvent {
    private String email;
    private String phone;
    private String userId;

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public UserMessageEntity(Object source) {
        super(source);
    }

    public UserMessageEntity(Object source, String email, String phone) {
        super(source);
        this.email = email;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "email:" + email + ",phone:" + phone;
    }
}

2. Create monitor 1 (observer 1)

/**
 * 监听发送邮件
 */
@Component
public class EmailListener implements ApplicationListener<UserMessageEntity> {
    /**
     * 监听的方法
     *
     * @param event
     */
    @Override
    @Async
    public void onApplicationEvent(UserMessageEntity event) {
        System.out.println(Thread.currentThread().getName() +"  邮件:" + event.toString());
    }
}

3. Create monitor 2 (observer 2)

/**
 * 监听发送邮件
 */
@Component
public class SmSListener implements ApplicationListener<UserMessageEntity> {
    /**
     * 监听的方法
     *
     * @param event
     */
    @Override
    @Async
    public void onApplicationEvent(UserMessageEntity event) {
        System.out.println(Thread.currentThread().getName() + "  短信:" + event.toString());
    }
}

4. Use Listener to monitor


@RestController
@Slf4j
public class OrderService {

    @RequestMapping("/addOrder2")
    public String addOrder2() {
       
        log.info("1.调用数据库下单订单代码:");
        UserMessageEntity userMessageEntity = new UserMessageEntity(this, "[email protected]", "1865891111");
        applicationEventPublisher.publishEvent(userMessageEntity);
        return "success";
    }
}

5. Execution effect

Insert picture description here

  • Some of the above content comes from the Ant Classroom http://www.mayikt.com/

  • Personal open source project (universal background management system) –> https://gitee.com/wslxm/spring-boot-plus2 , you can check it out if you like

  • This is the end of this article. If you find it useful, please like or pay attention to it. We will continue to update more content from time to time... Thank you for watching!

Guess you like

Origin blog.csdn.net/qq_41463655/article/details/108371356