springboot 同步解耦 异步化

  1. 云栖社区>
  2. 博客>
  3. 正文

springboot 同步解耦 异步化

curiousby 2018-03-23 11:38:00 浏览1214
展开阅读全文

 

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

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/



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

网友评论

登录后评论
0/500
评论
curiousby
+ 关注