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

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

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));


    }
}


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;
    }
}


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";
    }
}


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;
    }
}


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秒后输出的消息。

http://localhost:8080/event/success
 [  taskManager-1] com.wpw.iteratorpro.ContentListener      : 收到消息:success
 [  taskManager-1] com.wpw.iteratorpro.ContentListener      : result:success

上面的示例程序在应用中太少了,所以简单做下了解吧,喜欢文章的可以关注转发公众号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值