业务解耦-事件监听模式的使用

2020-03-25 10:38:17 浏览数 (1)

事件监听机制有点类似于sub/pub模式,不过这个技术点也仅适用于单体应用的范围,分布式应用还是老老实实使用消息队列来进行吧。

代码语言:javascript复制
package com.wpw.iteratorpro;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pc
 */
@RestController
public class ContentController {
    @Autowired
    private ApplicationContext applicationContext;
    @GetMapping(value="/event/{content}")
    public void sendEvent(@PathVariable(value = "content") String content){
        applicationContext.publishEvent(new ContentEvent(this,content));

    }
}
代码语言:javascript复制
package com.wpw.iteratorpro;

import org.springframework.context.ApplicationEvent;

public class ContentEvent extends ApplicationEvent {
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ContentEvent(Object source) {
        super(source);
    }

    public ContentEvent(Object source, String content) {
        super(source);
        this.content = content;
    }
}
代码语言:javascript复制
package com.wpw.iteratorpro;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ContentListener {
    private static final Logger logger = LoggerFactory.getLogger(ContentListener.class);

    @EventListener
    @Async
    public String handler(ContentEvent contentEvent) {
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            logger.error("error message:{}", e);
        }
        logger.info("收到消息:{}", contentEvent.getContent());
        if (EnumType.EnumType.getName().equals(contentEvent.getContent())) {
/**
 * 业务逻辑
 */
            logger.info("result:{}", contentEvent.getContent());
        }
        return "ok";
    }
}
代码语言:javascript复制
package com.wpw.iteratorpro;

public enum EnumType {
    EnumType("success", 200);
    private String name;
    private Integer code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    private EnumType(String name) {
        this.name = name;
    }

    EnumType(String name, Integer code) {
        this.name = name;
        this.code = code;
    }
}
代码语言:javascript复制
package com.wpw.iteratorpro;

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

@SpringBootApplication
@EnableAsync
public class IteratorProApplication {

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

}

启动示例程序,访问下面的地址,可以观测控制台10秒后输出的消息。

代码语言:javascript复制
http://localhost:8080/event/success
代码语言:javascript复制
 [  taskManager-1] com.wpw.iteratorpro.ContentListener      : 收到消息:success
 [  taskManager-1] com.wpw.iteratorpro.ContentListener      : result:success上面的示例程序在应用中太少了,所以简单做下了解吧,喜欢文章的可以关注转发公众号。

0 人点赞