当实现ApplicationListener接口的监听器

添加上如下这个类,会使监听器中onApplicationEvent()方法,以异步的方法执行

package xxx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

@Configuration
public class AsyncEventConfig {

    @Bean(name = "applicationEventMulticaster")
    public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
        SimpleApplicationEventMulticaster eventMulticaster
                = new SimpleApplicationEventMulticaster();

        eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return eventMulticaster;
    }
}

 

//自定义事件类
package xxx.init;

import org.springframework.context.ApplicationEvent;

public class MyApplicationEvent extends ApplicationEvent {
    private String message;
    public MyApplicationEvent(Object source,String message) {
        super(source);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

//自定义监听器

package xxx.init;

import xxx.constant.Tag;
import xxx.impl.MQFactory;
import xxx.interfaces.IMQComsumer;
import xxx.interfaces.IMQProducer;
import xxx.interfaces.Ibusiness;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;

@Component
public class MyListener implements ApplicationListener<MyApplicationEvent> {

    @Override
    public void onApplicationEvent(MyApplicationEvent event) {

        System.out.println(event.getMessage());
        System.out.println("bbbbbbbbbbbbb");

        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

 

 

 

package xxx.init;

import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
//该监听器支持异步处理即doMethod方法是异步执行,不影响service中的方法,是在不同的线程中执行的。
//需要在springboot的启动类上开启@EnableAsync注解
@Component
public class PtListener {
    @Async
    @EventListener
    public void doMethod(MyApplicationEvent event){

        event.getMessage();
        while (true){
            System.out.println("in listener method");
        }


    }
}

 

 

package xxx.web;

import xxx.init.MyApplicationEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private ApplicationEventPublisher context;
    @GetMapping("/getTest")
    public String testMethod(){
        MyApplicationEvent demoEvent = new MyApplicationEvent(this,"测试消息");
        context.publishEvent(demoEvent);//发送一个消息(即触发一个MyApplicationEvent事件,会触发监听器PtListener 的doMethod方法执行或者触发MyListener类的onApplicationEvent方法执行)
        System.out.println("main()");
        return "abc";
    }
}
/


package xxx.init;


import xxx.constant.Tag;
import xxx.impl.MQFactory;
import xxx.interfaces.IMQComsumer;
import xxx.interfaces.IMQProducer;
import xxx.interfaces.Ibusiness;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;

//监听ContextRefreshedEvent事件时,有可能会被触发多次。比如说,在我们的service中@AutoWired第三方的jar包中的feignClient
时。可以通过if(contextRefreshedEvent.getApplicationContext().getParent().getId().equals("bootstrap"))这个条件判断来过滤掉这些多余的触发调用。只调用一次onApplicationEvent方法。
public class ProduceAndConsumerInit implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if(contextRefreshedEvent.getApplicationContext().getParent().getId().equals("bootstrap"))
        {
            String type = contextRefreshedEvent.getApplicationContext().getEnvironment().getProperty("mq.Type");
            String address = contextRefreshedEvent.getApplicationContext().getEnvironment().getProperty("rocketmq.nameServeraddr");
            String topic = contextRefreshedEvent.getApplicationContext().getEnvironment().getProperty("rocketmq.topic");

            IMQProducer producer = MQFactory.getMQProducer(type,address,topic+"_"+ Tag.getName(5));
            try {
                producer.start();
            } catch (MQClientException e) {
                e.printStackTrace();
            }

            Ibusiness businessImpl = (Ibusiness)contextRefreshedEvent.getApplicationContext().getBean("serviceBusinessImpl");
            IMQComsumer comsumer = MQFactory.getMQComsumer(type,address, businessImpl,topic+"_"+Tag.getName(4),topic, Tag.getName(4), Tag.getName(5),true, producer);          
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值