您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

springboot怎么开启一个监听线程执行任务

2024/3/31 1:43:31发布8次查看
springboot开启一个监听线程执行任务public class startapplicationlistener implements applicationlistener<contextrefreshedevent> { @override public void onapplicationevent(contextrefreshedevent contextrefreshedevent) { applicationcontext applicationcontext = contextrefreshedevent.getapplicationcontext(); applicationcontext parent = applicationcontext.getparent(); if (parent == null) { cachemanager cachemanager = applicationcontext.getbean(cachemanager.class); cache cache = cachemanager.getcache(msconstants.node_cache_name); new thread(new runnable() { @override public void run() { //代码 } }).start(); } }
public static void main(string[] args) { springapplication app = new springapplication(figureserverapplication.class); app.addlisteners(new startapplicationlistener()); app.run(args);}
springboot中监听事件的创建流程在spring boot项目中,您可以使用注解@async来实现异步处理,而事件监听有同步和异步两种方式可供选择。
事件的大体步骤1:定义一个事件。
2: 创建一个监听器。
3:确保监听器在容器内。
4:发布事件,进行测试。
废话不多说,简单叙述几个不同方式的demo。
首先,我们先创建一个springboot项目。
第一种1:定义一个事件,需要继承applicationevent
public class myevent extends applicationevent { public myevent(object source) { super(source); system.out.println("我是一个事件,目前在事件的构造器内"); }}
2:创建一个监听器。直接实现applicationlistener接口,重写其中onapplicationevent方法.
public class myeventlistener implements applicationlistener<myevent> { @override public void onapplicationevent(myevent event) { system.out.println("现在开始执行监听器中的内容,我是直接实现applicationlistener接口的方式进行的"); system.out.println("event.getsource() = " + event.getsource()); }}
3:将监听器手动添加进应用容器内,并进行发布事件。
@springbootapplicationpublic class eventtestapplication { public static void main(string[] args) {// springapplication.run(eventtestapplication.class, args); //第一种:自己手动将监听器添加到application中 springapplication application = new springapplication(eventtestapplication.class); //添加监听器 application.addlisteners(new myeventlistener()); configurableapplicationcontext context = application.run(args); //进行发布事件 context.publishevent(new myevent("event数据源")); context.close(); }}
4:启动主启动类,进行打印测试。
第二种1:同第一种的1
2:创建一个监听器。
/** *第二种:打上compoent注解, 将事件监听器自动加入到应用容器中 * 这种方式不需要手动加入到容器中。 * */@componentpublic class myeventlistener2 implements applicationlistener<myevent> { @override public void onapplicationevent(myevent event) { system.out.println("现在开始执行监听器中的内容,我是打compoent注解的方式进行的"); system.out.println("event.getsource() = " + event.getsource()); }}
3:发布事件,后续进行启动类测试即可。
@springbootapplicationpublic class eventtestapplication { public static void main(string[] args) { configurableapplicationcontext context = springapplication.run(eventtestapplication.class, args); //发布事件 context.publishevent(new myevent("source")); context.close(); }}

第三种全部采用注解的方式
1:同第一种的1
2:创建一个监听器。
/** *第三种:采用@eventlistener注解的方式,不需要在类上实现applicationlistener接口。 * 直接采用的是通过注解,将方法标识为一个监听器。 * */@componentpublic class myeventlistener3 { @async//异步注解。开启一个新线程。 去掉此注解则会变成同步监听。 @eventlistener(classes = myevent.class) public void testlistener(myevent myevent){ system.out.println("我是@eventlistener注解的方式实现的监听器"); system.out.println("myevent.getsource() = " + myevent.getsource()); }}
3:发布事件,后续进行启动类测试即可。
@springbootapplicationpublic class eventtestapplication { public static void main(string[] args) { configurableapplicationcontext context = springapplication.run(eventtestapplication.class, args); //发布事件 context.publishevent(new myevent("source")); context.close(); }}

下面是代码的结构图:
以上就是springboot怎么开启一个监听线程执行任务的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product