欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合websocket整合應(yīng)用示例(下)

 更新時間:2016年04月05日 09:59:31   作者:Bridge Li  
這篇文章主要介紹了Spring整合websocket整合應(yīng)用示例(下)的相關(guān)資料,需要的朋友可以參考下

在Spring整合websocket整合應(yīng)用示例(上)文章中,我們已經(jīng)實(shí)現(xiàn)了websocket,但還有一個核心的業(yè)務(wù)實(shí)現(xiàn)類沒有實(shí)現(xiàn),這里我們就實(shí)現(xiàn)這個業(yè)務(wù)核心類,因?yàn)槔戏騾⑴c的這個系統(tǒng)使用websocket發(fā)送消息,所以其實(shí)現(xiàn)就是如何發(fā)送消息了。

7. NewsListenerImpl的實(shí)現(xiàn)

package cn.bridgeli.websocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lagou.common.base.util.date.DateUtil;
import com.lagou.platform.news.api.enumeration.PlatNewsCategoryType;
import com.lagou.platform.news.web.dao.ext.model.PlatNewsVo;
import com.lagou.platform.news.web.dao.ext.model.SearchCondition;
import com.lagou.platform.news.web.quartz.impl.TimingJob;
import com.lagou.platform.news.web.service.PlatNewsService;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Description : 站內(nèi)消息監(jiān)聽器實(shí)現(xiàn)
* @Date : 16-3-7
*/
@Component
public class NewsListenerImpl implements NewsListener{
private static final Logger logger = LoggerFactory.getLogger(NewsListenerImpl.class);
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
//線程池
private ExecutorService executorService = Executors.newCachedThreadPool();
//任務(wù)調(diào)度
private SchedulerFactory sf = new StdSchedulerFactory();
@Autowired
private PlatNewsService platNewsService;
@Override
public void afterPersist(PlatNewsVo platNewsVo) {
logger.info("監(jiān)聽到有新消息添加。。。");
logger.info("新消息為:"+gson.toJson(platNewsVo));
//啟動線程
if(null != platNewsVo && !StringUtils.isBlank(platNewsVo.getCurrentoperatoremail())){
//如果是定時消息
if(platNewsVo.getNewsType() == PlatNewsCategoryType.TIMING_TIME.getCategoryId()){
startTimingTask(platNewsVo); //定時推送
}else{
//立即推送
executorService.execute(new AfterConnectionEstablishedTask(platNewsVo.getCurrentoperatoremail()));
}
}
}
@Override
public void afterConnectionEstablished(String email) {
logger.info("建立websocket連接后推送新消息。。。");
if(!StringUtils.isBlank(email)){
executorService.execute(new AfterConnectionEstablishedTask(email));
}
}
/**
* @Description : 如果新添加了定時消息,啟動定時消息任務(wù)
* @param platNewsVo
*/
private void startTimingTask(PlatNewsVo platNewsVo){
logger.info("開始定時推送消息任務(wù)。。。");
Date timingTime = platNewsVo.getTimingTime();
if(null == timingTime){
logger.info("定時消息時間為null。");
return;
}
logger.info("定時推送任務(wù)時間為:"+DateUtil.date2String(timingTime));
JobDetail jobDetail= JobBuilder.newJob(TimingJob.class)
.withIdentity(platNewsVo.getCurrentoperatoremail()+"定時消息"+platNewsVo.getId(), "站內(nèi)消息")
.build();
//傳遞參數(shù)
jobDetail.getJobDataMap().put("platNewsService",platNewsService);
jobDetail.getJobDataMap().put("userEmail",platNewsVo.getCurrentoperatoremail());
Trigger trigger= TriggerBuilder
.newTrigger()
.withIdentity("定時消息觸發(fā)"+platNewsVo.getId(), "站內(nèi)消息")
.startAt(timingTime)
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(0) //時間間隔
.withRepeatCount(0) //重復(fù)次數(shù)
)
.build();
//啟動定時任務(wù)
try {
Scheduler sched = sf.getScheduler();
sched.scheduleJob(jobDetail,trigger);
if(!sched.isShutdown()){
sched.start();
}
} catch (SchedulerException e) {
logger.info(e.toString());
}
logger.info("完成開啟定時推送消息任務(wù)。。。");
}
/**
* @Description : 建立websocket鏈接后的推送線程
*/
class AfterConnectionEstablishedTask implements Runnable{
String email ;
public AfterConnectionEstablishedTask(String email){
this.email = email;
}
@Override
public void run() {
logger.info("開始推送消息給用戶:"+email+"。。。");
if(!StringUtils.isBlank(email)){
SearchCondition searchCondition = new SearchCondition();
searchCondition.setOperatorEmail(email);
JSONArray jsonArray = new JSONArray();
for(PlatNewsCategoryType type : PlatNewsCategoryType.values()){
searchCondition.setTypeId(type.getCategoryId());
int count = platNewsService.countPlatNewsByExample(searchCondition);
JSONObject object = new JSONObject();
object.put("name",type.name());
object.put("description",type.getDescription());
object.put("count",count);
jsonArray.add(object);
}
if(null != jsonArray && jsonArray.size()>0){
UserSocketVo userSocketVo = WSSessionLocalCache.get(email);
TextMessage reMessage = new TextMessage(gson.toJson(jsonArray));
try {
if(null != userSocketVo){
//推送消息
userSocketVo.getWebSocketSession().sendMessage(reMessage);
//更新推送時間
userSocketVo.setLastSendTime(DateUtil.getNowDate());
logger.info("完成推送新消息給用戶:"+userSocketVo.getUserEmail()+"。。。");
}
} catch (IOException e) {
logger.error(e.toString());
logger.info("站內(nèi)消息推送失敗。。。"+e.toString());
}
}
}
logger.info("結(jié)束推送消息給"+email+"。。。");
}
}
}

這個類就是websocket的核心業(yè)務(wù)的實(shí)現(xiàn),其具體肯定和業(yè)務(wù)相關(guān),由于業(yè)務(wù)的不同,實(shí)現(xiàn)肯定不同,因?yàn)槔戏騾⑴c的系統(tǒng)是發(fā)送消息,所以里面最核心的一句就是:

userSocketVo.getWebSocketSession().sendMessage(reMessage);

通過WebSocketSession的sendMessage方法把我們的消息發(fā)送出去。另外,這主要是后端的實(shí)現(xiàn),至于前端的實(shí)現(xiàn),因?yàn)槔戏蚴呛蠖顺绦蛟潮容^關(guān)注后端,所以前端就不多做介紹了,大家可以自己去網(wǎng)上查資料。最后需要說明的是,老夫之前搜一些學(xué)習(xí)資料的時候,發(fā)現(xiàn)老夫該同事的寫法和有一篇文章幾乎一樣,我想該同事應(yīng)該是參考了這篇文章,所以列在下面,算作參考資料。

相關(guān)文章

  • Maven發(fā)布Jar包中文亂碼解決方法

    Maven發(fā)布Jar包中文亂碼解決方法

    這篇文章主要介紹了Maven發(fā)布Jar包中文亂碼解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • java 排序算法之希爾算法

    java 排序算法之希爾算法

    這篇文章主要介紹了java 排序算法之希爾排序,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Java中ArrayList去除重復(fù)元素(包括字符串和自定義對象)

    Java中ArrayList去除重復(fù)元素(包括字符串和自定義對象)

    本文主要介紹了Java中ArrayList去除重復(fù)元素(包括字符串和自定義對象)的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • SpringMVC 單文件上傳與多文件上傳實(shí)例

    SpringMVC 單文件上傳與多文件上傳實(shí)例

    這篇文章主要介紹了SpringMVC 單文件上傳與多文件上傳實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • SpringBoot?@InitBinder注解綁定請求參數(shù)的過程詳解

    SpringBoot?@InitBinder注解綁定請求參數(shù)的過程詳解

    這篇文章主要介紹了SpringBoot?@InitBinder注解綁定請求參數(shù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • MyBatis中有關(guān)int和Integer的使用方式

    MyBatis中有關(guān)int和Integer的使用方式

    這篇文章主要介紹了MyBatis中有關(guān)int和Integer的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java面向?qū)ο蠡A(chǔ)知識之委托和lambda

    Java面向?qū)ο蠡A(chǔ)知識之委托和lambda

    這篇文章主要介紹了Java面向?qū)ο蟮闹泻?lambda,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-11-11
  • 貪心算法原理及在Java中的使用

    貪心算法原理及在Java中的使用

    我們可能在好多地方都會聽到貪心算法這一概念,并且它的算法思想也比較簡單就是說算法只保證局部最優(yōu),進(jìn)而達(dá)到全局最優(yōu)。但我們實(shí)際編程的過程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問題必須滿足貪心選擇性質(zhì)
    2021-05-05
  • Java中Collection與Collections的區(qū)別詳解

    Java中Collection與Collections的區(qū)別詳解

    這篇文章主要為大家詳細(xì)介紹了Java中Collection與Collections的區(qū)別,文中有詳細(xì)的代碼示例,具有一定的參考價值,感興趣的同學(xué)可以參考一下
    2023-06-06
  • Java中數(shù)組的使用與注意事項(xiàng)詳解(推薦)

    Java中數(shù)組的使用與注意事項(xiàng)詳解(推薦)

    數(shù)組是一組地址連續(xù)、長度固定的具有相同類型的數(shù)據(jù)的集合,通過數(shù)組下標(biāo)我們可以指定數(shù)字中的每一個元素,下面這篇文章主要給大家介紹了關(guān)于Java中數(shù)組的使用與注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評論