Spring Integration WebSocket Integration Applicationの例(パート1)では、WebSocketを実装しましたが、実装されていないコアビジネス実装クラスがまだあります。ここでは、このビジネスコアクラスを実装します。私が参加したシステムは、WebSocketを使用してメッセージを送信するため、実装はメッセージを送信する方法です。
7。NewsListenerimplの実装
パッケージ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.platnewscategtype; 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; 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.slf4j.loggerfactory; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; org.springframework.web.web.sock.textmasage; import org.springframework.sock.textmas.io.io.io.io.io.io.io. java.util.date; import java.util.list; Import java.util.concurrent.executorservice; Import java.util.concurrent.executors;/*** @description:in-siteメッセージリスナー実装*@date:16-3-7*/ @componentpublic classpublic classpublic classpublic classedpublic classedpublic classedpublic classe = loggerfactory.getLogger(NewsListenerimpl.Class); gson gson = new gsonbuilder()。setDateFormat( "yyyy-mm-dd hh:mm:ss")。 stdschedulerfactory();@autowiredprivate platnewsservice platnewsservice; @overridepublic void afterpersist(platnewsvo platnewsvo){logger.info( "新しいメッセージが追加されたのを聞いた。 。 。 "); logger.info("新しいメッセージは: "+gson.tojson(platnewsvo)) platnewscategorytype.timing_time.getcategoryid()){starttimingtask(platnewsvo); afterconnectioneStabled(string email){logger.info( "Websocket Connectionを確立した後に新しいメッセージを押します..."); if(!stringutils.isblank(email){execustorservice.execute(new afterconnectionEstableishedtask(email);}}/*** @description:@description:@description themed pirting added added firtin starttimingtask(platnewsvo platnewsvo){logger.info( "タイミングプッシュメッセージタスクを開始...");日付タイミング= platnewsvo.gettimingtime(); if(null ==タイミングタイム){logger.info( "タイムされたメッセージはnull。 "+dateutil.date2string(タイミングタイム)); jobdetail jobdetail = jobbuilder.newjob(timingjob.class) jobdetail.getjobdatamap()。put( "platnewsservice"、platnewsservice); jobdetail.getjobdatamap() "+platnewsvo.getid()、" in-site message ")。startat(timingtime).withschedule(simpleschedulebuilder.simplesschedule()。 {scheduler sched = sf.getscheduler(); sched.schedulejob(jobdetail、trigger); if(!sched.isshutdown()){sched.start();}} catch(schedulerexception e){logger.info(e.toString()); 。 。 ");}/*** @description:WebSocketリンクを確立した後にスレッドを押します*/class AfterConnectionSectionAbleishedTaskはrunnable {string email; public andconnectioneStableishedtask(this.email = email;} @overridepublic void run(){logger.info(){logger.info("ユーザーへのプッシュメッセージを開始する:ユーザーへのプッシュメッセージを開始する"+email+" .... "); if(!stringutils.isblank(email)){searchCondition searchCondition = new searchCondition(); searchCondition.setoperatorEmail(email); jsonarray jsonarray = new jsoneray(); 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) Remessage = new TextMessage(gson.tojson(jsonarray)); try {if(null!= usersockvo){// push message usersocketvo.getWebsocketsession()。 "+usersocketvo.getuseremail()+"。 。 。 ");}} catch(ioException e){logger.error(e.tostring()); logger.info(" in-site message push failed ... "+e.tostring();}}} enfo(" end push sump to "+email+" .... ");}}}このクラスは、WebSocketのコアビジネスの実装です。その特定の確実性はビジネスに関連しています。ビジネスが異なるため、実装は間違いなく異なります。私が参加したシステムはメッセージを送信することであるため、最もコア文は次のとおりです。
usersocketvo.getWebsocketsession()。sendmessage(remessage);
WebSocketessessionのsendMessageメソッドを介してメッセージを送信します。さらに、これは主にバックエンドの実装です。フロントエンドの実装については、私はバックエンドプログラマーであるため、バックエンドにもっと注意を払うので、フロントエンドは導入しません。自分でオンラインで情報を確認できます。最後に、以前にいくつかの学習資料を検索したとき、私の同僚の執筆スタイルは記事とほぼ同じであることがわかりました。同僚はこの記事を参照すべきだったので、以下にリストされ、参考資料としてカウントされます。