码迷,mamicode.com
首页 > 编程语言 > 详细

spring事件

时间:2018-11-16 10:58:25      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:read   div   turn   bean   线程   get   extend   stat   art   

DemoEvent 自定义事件 继承ApplicationEvent

/**
 * 自定义事件
 */
public class DemoEvent extends ApplicationEvent {

    private String msg;
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public DemoEvent(Object source, String msg, String userName) {
        super(source);
        this.msg = msg;
        this.userName  = userName;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

DemoListener 监听 实现ApplicationListener 接口的onApplicationEvent 方法

 @Async
    public void onApplicationEvent(DemoEvent event) {
        //模拟业务耗时
        try {
            System.out.println("ThreadName:"+Thread.currentThread().getName());
            System.out.println("消息:"+event.getMsg()+"发送给了"+event.getUserName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

DemoPublisher 发布 实现ApplicationEventPublisherAware 接口 的setApplicationEventPublisher 方法 并提供发布方法

public class DemoPublisher implements ApplicationEventPublisherAware{

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    private ApplicationEventPublisher applicationEventPublisher;

    public  void publish(String msg,String userName){
        applicationEventPublisher.publishEvent(new DemoEvent(this,msg,userName));
    }
}

  App 类

public class App {
    
    private static ClassPathXmlApplicationContext applicationContext;

    public static void main(String[] args) {
        applicationContext = new ClassPathXmlApplicationContext("event/beans.xml");
        final DemoPublisher publisher = (DemoPublisher)applicationContext.getBean("demoPublisher");
        publisher.publish("消息1","张三");
        publisher.publish("消息2","张三");
        publisher.publish("消息3","李四");   
        Thread t1 = new Thread(){
            public void run(){
                publisher.publish("消息11","王五");
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                publisher.publish("消息22","二麻子");
            }
        };
        t1.start();
        t2.start();
        System.out.println("主线程"+Thread.currentThread().getName()+"结束");
        
    }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="demoListener" class="event.DemoListener"></bean>

    <bean id="demoPublisher" class="event.DemoPublisher"></bean>
</beans>

测试结果

ThreadName:main
消息:消息1发送给了张三
ThreadName:main
消息:消息2发送给了张三
ThreadName:main
消息:消息3发送给了李四
主线程main结束
ThreadName:Thread-0
消息:消息11发送给了王五
ThreadName:Thread-1
消息:消息22发送给了二麻子

异步执行了发送消息

spring事件

标签:read   div   turn   bean   线程   get   extend   stat   art   

原文地址:https://www.cnblogs.com/diaobiyong/p/9967945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!