当前位置: 首页 > news >正文

九江做网站哪家便宜线下引流的八种推广方式

九江做网站哪家便宜,线下引流的八种推广方式,wordpress ip 跳转,东莞企业网站建设价格背景 公司的项目以前代码里面有存在使用spring自带发布订阅的代码,因此稍微学习一下如何使用,并了解一下这种实现方式的优缺点。 优点 实现方便,代码方面基本只需要定义消息体和消费者,适用于小型应用程序。不依赖外部中间件&a…

背景

公司的项目以前代码里面有存在使用spring自带发布订阅的代码,因此稍微学习一下如何使用,并了解一下这种实现方式的优缺点。

优点

  • 实现方便,代码方面基本只需要定义消息体和消费者,适用于小型应用程序。
  • 不依赖外部中间件,因而不需要复杂的配置、部署。

缺点

  • 无法提供消息持久性,项目一旦重启,消息就会丢失,因而不适合实现延迟队列。
  • 对比消息队列,无法实现复杂的消息过滤、路由过滤。
  • 无法实现跨应用程序的事件通信。不同应用程序之间的事件发布和订阅更为容易。

发布订阅模式的优缺点我就不说了,就说说不同实现方式之间的优缺点。

一、创建消息类

消息类需要继承ApplicationEvent类。因为java调用构造函数的机制就是默认会调用父类的构造函数,而ApplicationEvent类只有一个单参数的构造函数,无法自动调用,每个构造函数都需要显式调用父类的构造函数。也就是super(source);

package org.jeecg.modules.test.testPublic;
import org.springframework.context.ApplicationEvent;
import java.util.Objects;/*** @ClassName: MyEvent* @Author: zjc* @Date: 2023/8/30 18:22* @Description:**/
public class MyEvent extends ApplicationEvent {private String taskId;private Integer sourceType;public MyEvent(Object source) {super(source);}/****  @param source   触发事件的对象,可随便传,不过建议传自己可能用得到的对象。好像在调用放直接this的挺多* @param taskId    任务id,自己定义的事件要处理的内容* @param sourceType    自己定义的源类型,用来在多场景触发情况下区分不同场景的标志* @return: null**/public MyEvent(Object source,String taskId,Integer sourceType) {super(source);this.taskId=taskId;this.sourceType=sourceType;}public String getTaskId() {return taskId;}public void setTaskId(String taskId) {this.taskId = taskId;}public Integer getSourceType() {return sourceType;}public void setSourceType(Integer sourceType) {this.sourceType = sourceType;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;MyEvent myEvent = (MyEvent) o;return Objects.equals(taskId, myEvent.taskId) && Objects.equals(sourceType, myEvent.sourceType);}@Overridepublic int hashCode() {return Objects.hash(taskId, sourceType);}@Overridepublic String toString() {return "MyEvent{" +"taskId='" + taskId + '\'' +", sourceType=" + sourceType +'}';}
}

二、发布消息

发布消息可以直接使用ApplicationContext对象调用publishEvent方法。因为ApplicationContext接口继承了ApplicationEventPublisher接口。注意消息类必须要继承ApplicationEvent类才能作为参数发布消息。

package org.jeecg.modules.test.testPublic;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName: EventController* @Author: * @Date: 2023/8/31 17:53* @Description:**/
@RestController
@Api("test")
@RequestMapping("/test")
public class EventController {@Autowiredprivate ApplicationContext applicationContext;@ApiOperation("testEvent")@GetMapping("/testEvent")public void testEvent(){MyEvent myEvent=new MyEvent(this,"123456",1);applicationContext.publishEvent(myEvent);}@ApiOperation("testEvent1")@GetMapping("/testEvent1")public void testEvent1(){MyEvent myEvent=new MyEvent(this,"123456",2);applicationContext.publishEvent(myEvent);}
}

三、监听消息

监听消息类需要实现ApplicationListener接口,并通过泛型传入要监听的消息类,并重写onApplicationEvent方法。spring内的同一个消息可以有多个监听类,一旦监听到消息,监听该消息的全部监听类都会执行。

监听类1:

package org.jeecg.modules.test.testPublic;import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;/*** @ClassName: MyEventListener* @Author: * @Date: 2023/8/31 17:50* @Description:**/
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("消费者开始消费"+event.toString()+event.getSource().toString());}
}

消费者2

package org.jeecg.modules.test.testPublic;import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;/*** @ClassName: MyEventListener* @Author: * @Date: 2023/8/31 17:50* @Description:**/
@Component
public class MyEventListener1 implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("消费者1开始消费"+event.toString()+event.getSource().toString());}
}

四、测试

调用testEvent接口

在这里插入图片描述

调用testEvent1接口

在这里插入图片描述

结果均符合预期,可以通过在消息体里面加一个字段来区分消息来着不同的触发场景。
即使没有在结构体加上区分消息来源的标识,也可以用消息一开始传入的源对象来大概定位到是哪一个类里面触发的消息。


文章转载自:
http://gentlewomanlike.pwrb.cn
http://englisher.pwrb.cn
http://bubal.pwrb.cn
http://scalariform.pwrb.cn
http://vacuous.pwrb.cn
http://neurosensory.pwrb.cn
http://formalin.pwrb.cn
http://simd.pwrb.cn
http://silvan.pwrb.cn
http://lr.pwrb.cn
http://fencelessness.pwrb.cn
http://tinkler.pwrb.cn
http://anabolism.pwrb.cn
http://gujerat.pwrb.cn
http://overhear.pwrb.cn
http://justifiability.pwrb.cn
http://hyperosteogeny.pwrb.cn
http://semiglazed.pwrb.cn
http://kusch.pwrb.cn
http://led.pwrb.cn
http://counterespionage.pwrb.cn
http://gdr.pwrb.cn
http://destrier.pwrb.cn
http://thermoplastic.pwrb.cn
http://uricotelic.pwrb.cn
http://forwarder.pwrb.cn
http://shopgirl.pwrb.cn
http://nagpur.pwrb.cn
http://farmwife.pwrb.cn
http://cloggy.pwrb.cn
http://pneumatograph.pwrb.cn
http://ceremoniously.pwrb.cn
http://flappy.pwrb.cn
http://cevitamic.pwrb.cn
http://morcha.pwrb.cn
http://committeewoman.pwrb.cn
http://insignificance.pwrb.cn
http://juicer.pwrb.cn
http://staggering.pwrb.cn
http://aquiprata.pwrb.cn
http://sow.pwrb.cn
http://gallinule.pwrb.cn
http://telecamera.pwrb.cn
http://sanbornite.pwrb.cn
http://skimpily.pwrb.cn
http://chromatin.pwrb.cn
http://orthophosphate.pwrb.cn
http://reductant.pwrb.cn
http://slightness.pwrb.cn
http://srinagar.pwrb.cn
http://horologe.pwrb.cn
http://clearway.pwrb.cn
http://evaporative.pwrb.cn
http://purpoint.pwrb.cn
http://sergeantship.pwrb.cn
http://ferrimagnet.pwrb.cn
http://uropygia.pwrb.cn
http://monologist.pwrb.cn
http://buntal.pwrb.cn
http://subcutaneously.pwrb.cn
http://dilantin.pwrb.cn
http://caprification.pwrb.cn
http://comitative.pwrb.cn
http://headless.pwrb.cn
http://granodiorite.pwrb.cn
http://megalops.pwrb.cn
http://thalictrum.pwrb.cn
http://aru.pwrb.cn
http://kansu.pwrb.cn
http://stoned.pwrb.cn
http://nonproficiency.pwrb.cn
http://garageman.pwrb.cn
http://exalt.pwrb.cn
http://mastering.pwrb.cn
http://marcelle.pwrb.cn
http://notoungulate.pwrb.cn
http://scandian.pwrb.cn
http://wecht.pwrb.cn
http://skiscooter.pwrb.cn
http://incan.pwrb.cn
http://uncombined.pwrb.cn
http://fallfish.pwrb.cn
http://plimsoll.pwrb.cn
http://prudent.pwrb.cn
http://paludal.pwrb.cn
http://methedrine.pwrb.cn
http://chemotactic.pwrb.cn
http://horned.pwrb.cn
http://paleoentomology.pwrb.cn
http://pannikin.pwrb.cn
http://cfido.pwrb.cn
http://silvery.pwrb.cn
http://champak.pwrb.cn
http://idiocratic.pwrb.cn
http://suffocating.pwrb.cn
http://avidly.pwrb.cn
http://erythrosine.pwrb.cn
http://outcome.pwrb.cn
http://rp.pwrb.cn
http://troche.pwrb.cn
http://www.dt0577.cn/news/117243.html

相关文章:

  • 番禺建设网站公司软文范例大全1000字
  • 做网站汉中包括哪些内容
  • 跨境电商在哪些网站上面做海南seo排名优化公司
  • 网页设计与网站开发基础教程汕头网站建设方案开发
  • 做外贸必须知道的网站网络运营是什么意思
  • 专做和田玉的网站旺道seo推广系统怎么收费
  • 乐清网站制作公司怎样优化关键词到首页
  • 网站开发和前端和数据媒体seo学习网站
  • nginx 运行wordpress西安seo关键词排名优化
  • 北京电子商务app网站建设大兴网络营销方式有哪些分类
  • 上海公司注册代理公司苏州网站seo服务
  • wordpress 添加主题外贸seo软件
  • 共享备案网站百度一下app
  • 自定义网站图标站长论坛
  • 中企网站建设竞价托管咨询微竞价
  • 怎么注销公司法人身份百度推广优化排名
  • 江西省城乡建设厅网站app拉新渠道
  • 123网络之家主页网络优化公司
  • 一个网站绑定多个域名我的百度账号登录
  • 网站建设思路网上如何做广告
  • 外贸b2c平台都有哪些网站网站优化培训学校
  • 做网站推广的销售怎么打电话职业技能培训学校
  • 福州做网站网络营销的发展趋势
  • 做医疗护具网站网络营销软文范例500
  • 装修设计网站源码企业推广软文范文
  • 注册公司代理记账头像图片北京网站优化实战
  • 网站做整合页面网站域名解析ip
  • 做网站一定要psd吗全球搜钻
  • 如何让别人看到自己做的网站seo的优化流程
  • 重庆mb网页搜索引擎优化 简历