springboot 同步解耦 异步化

简介:    ---------------------------------------------------------------------------------------------------------------- springboot 详解 (一) hellowo...

 

 ----------------------------------------------------------------------------------------------------------------

springboot 详解 (一) helloworld                                    下载demo          

springboot 详解 (二) crud                                             下载demo     

springboot 详解 (三) 多数据源                                        下载demo      

springboot 详解 (四)redis & filter                                    下载demo    

springboot 详解 (五)interceptor                                       下载demo     

springboot 详解 (六)servlet & scheduled & listener         下载demo     

springboot 详解(七) dubbox & zookeeper   下载(productor) 下载(constumser)

springboot 同步解耦 异步化 下载demo

springboot jenkins docker 部署 

springboot 详解(八) springboot & springcloud 

----------------------------------------------------------------------------------------------------------------

 

 

 

代码下载位置https://download.csdn.net/download/knight_black_bob/10309215

 

 



 

 

 

public class MessageEvent extends ApplicationEvent{

	private MSMMessageEnitity entity;
	private boolean flag;
	public MessageEvent(Object source,MSMMessageEnitity entity, boolean flag) {
		super(source);
		this.entity = entity;
	}

	public MSMMessageEnitity getEntity() {
		return entity;
	}

	public void setEntity(MSMMessageEnitity entity) {
		this.entity = entity;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	 

}

 

public class MSMMessageEnitity  implements Serializable{

	 private String msgId;
	 private Date sendDate;
	 private String content;
	 private String sendUserid;
	 
	 
	public String getMsgId() {
		return msgId;
	}
	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}
	public Date getSendDate() {
		return sendDate;
	}
	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getSendUserid() {
		return sendUserid;
	}
	public void setSendUserid(String sendUserid) {
		this.sendUserid = sendUserid;
	}
	 
	 

}

 

@Component
public class MessageListener implements ApplicationListener {

	@Async
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof MessageEvent) {
			sendMsmMessage();
		}
	}

	private void sendMsmMessage() {
		System.out.println("======================= send evnetlistener    start =======================");
		try {
			Thread.sleep(3000);
			//TODO: 
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("======================= send evnetlistener    success =======================");		
	}

 
}

 

 

@Service
public class SendMessageServiceImpl  implements SendMessageService{

	@Autowired
	private ApplicationContext context;
	
	@Override
	public void send(MSMMessageEnitity entity) {
		 context.publishEvent(new MessageEvent(this, entity,true));
	}

	 

}

 

 

public interface SendMessageService {

	 public void send(MSMMessageEnitity entity);

}

 

@RestController
@RequestMapping("/messages")
public class MessageSendController {

	@Resource
	SendMessageService sendMessageService;
	
	
	@RequestMapping("/send")
	 public void send(){
		MSMMessageEnitity entity = new MSMMessageEnitity();
		sendMessageService.send(entity );
		System.out.println(" ================insert db=========================");
		System.out.println("=====================end= end=======================");
	 }
}

 

 

@EnableAsync
@SpringBootApplication 
public class SyncEvnetApplication {
	  
		public static void main(String[] args) {
			SpringApplication.run(SyncEvnetApplication.class, args);
		}
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!

目录
相关文章
|
1月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
5月前
|
Java
springboot使用异步任务
springboot使用异步任务
58 0
|
7月前
|
Java
springboot异步操作之Async
springboot异步操作之Async
124 1
|
16天前
|
Java Spring
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
19 0
|
1月前
|
JavaScript Java API
spring boot使用异步多线程
一文讲清楚spring boot如何结合异步多线程实现文件的导出这类耗时间的操作优化以及常用的场景,了解异步思想
28 0
spring boot使用异步多线程
|
5月前
|
消息中间件 Java Kafka
SpringBoot中使用异步方法优化Service逻辑,提高接口响应速度
异步方法适用于逻辑与逻辑之间可以相互分割互不影响的业务中, 如生成验证码和发送验证码组成的业务, 其实无需等到真正发送成功验证码才对客户端进行响应, 可以让短信发送这一耗时操作转为异步执行, 解耦耗时操作和核心业务;
|
4月前
|
Java
SpringBoot原理分析 | 任务:异步、邮件、定时
SpringBoot原理分析 | 任务:异步、邮件、定时
65 0
|
4月前
|
Java Spring
SpringBoot - @Async异步任务与线程池
SpringBoot - @Async异步任务与线程池
57 1
|
5月前
|
JavaScript Java Spring
Spring Boot集成thymeleaf异步刷新页面
Spring Boot集成thymeleaf异步刷新页面
121 0
|
7月前
|
缓存 Java 开发工具
SpringBoot执行异步任务Async介绍
当我们在使用SpringBoot进行开发的时候,可能会遇到一些执行异步任务的场景,如果每次执行这些异步任务都去新建一个异步线程来执行的话,那代码就太冗余了。幸好SpringBoot给我们提供了Async的注解,让我们能够很轻松地对这些异步任务进行执行。
213 3