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.latform.news.api.enumeration.platnewscategoryType; 가져 오기; com.lagou.platform.news.web.dao.ext.model.platnewsvo; import com.lagou.platform.news.web.dao.ext.model.searchCondition; import com.lagou.latform.web.web.quartz.impl.imingjob; 수입 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 org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.bean.beans.annotation.AntOntation.AUTOWERED; import org.spramframework.stereotyp.component; import org.spramework.web.supt.textmessage; import java.date java.utement; java.util.list; import java.util.concurrent.executorservice; import java.util.concurrent.executors;/*** @description : in-site 메시지 청취자 구현*@Date : 16-3-7*/ @componentpublic Class NewsListenerimpl 뉴스리스트리스트 {NewsListor emprivate logger = loggerfactory.getLogger (NewsListenerImpl.class); gson gson = new gsonBuilder (). setDateFormat ( "yyyy-mm-dd hh : mm : mm : ss"). create (); // 스레드 private executorService executorService = executorSERVICE = exector.newCachedThreadPool (); // taskoctorfactory sf. stdschedulerfactory ();@autowiredprivate platnewsservice platnewsservice; @overridepublic void afterpersist (platnewsvo platnewsvo) {logger.info ( "A가 추가 된 새로운 메시지가 추가된다. . . ". platnewscategoryType.timing_time.getCategoryID ()) {startTimingTask (platnewsvo); email) {logger.info ( "WebSocket 연결을 설정 한 후 새 메시지를 눌러 ..."); if (! stringUtils.isblank (email)) {executorService.Execute (new AfterConnectionStablishedTask (이메일)); startTimingTask (platnewsvo platnewsvo) {logger.info ( "타임 푸시 메시지 작업 시작 ..."); 날짜 타임 타임 = platnewsvo.gettimingtime (); if (null == timingtime) {logger.info ( "시간이 메시지 시간이 null입니다.") "+dateUtil.date2string (TimingTime)); jobDetail jobDetail = jobBuilder.newjob (TimingJob.class) .withEdentity (platnewsvo.getCurrentOpperatorEmail ()+"Timeed Message "+Platnewsvo.getId (),"사이트 메시지 "; jobDetail.getJobdatamap (). put ( "platnewsservice", platnewsservice); jobDetail.getJobdatamap (). put ( "userEmail", platnewsvo.getCurrentOperemail (); 트리거 = 트리거 builder.newtrigger () "+platnewsvo.getId (),"In-site Message "). startAt (timingtime) .withSchedule (simpleschedulebuilder.simpleschedule (). 내부 valinseconds (0) // time intergal.withRepeatcount (0) // repetitions of refetitions). {스케줄러 스케줄 = sf.getScheduler (); sched.schedulejob (jobDetail, trigger); if (! sched.isshutdown ()) {sched.start ();}} catch (schedulerexception e) {logger.info (e.tostring ()); . . ");}/*** @description : WebSocket 링크를 설정 한 후 푸시 스레드 링크*/클래스 애프터 콘센트 스ableStastASK는 runnable {string email; public AfterConnectionStablishedTask (string email) {this.email = email;} @revidepublic void run () {logger.info ("사용자에게 푸시 메시지를 시작합니다. "+email+".... "); if (! stringUtils.isblank (email))) {searchCondition SearchCondition = new SearchCondition (); SearchCondition.setOperatorEmail (email); 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) wssessionlocalcache.get (email); textMessage Repessage = new TextMessage (gson.tojson (jsonarray)); try {if (null! = userocketvo) {// using message userocketvo.getwebsocketsession (). sendmessage (reccessage); // 푸시 시간을 업데이트합니다. userocketvo.setLastSendTime (dateUtil.getNowDate ()); logger.info ( ""+userocketvo.getUserEmail ()+"에 새 메시지를 완료합니다. . . ");}} catch (ioexception e) {logger.error (e.toString ()); logger.info ("현장 메시지 푸시 실패 ... "+e.tostring ());}}} logger.info (" "+email+");}}}}}}}}이 클래스는 WebSocket의 핵심 비즈니스를 구현하는 것입니다. 구체적인 확실성은 비즈니스와 관련이 있습니다. 다른 비즈니스로 인해 구현은 분명히 다릅니다. 내가 참여한 시스템은 메시지를 보내기 때문에 가장 핵심 문장은 다음과 같습니다.
userocketvo.getwebsocketsession (). sendMessage (개선);
WebSocketSession의 SendMessage 메소드를 통해 메시지를 보냅니다. 또한 이것은 주로 백엔드의 구현입니다. 프론트 엔드 구현과 관련하여 백엔드 프로그래머이기 때문에 백엔드에 더 많은 관심을 기울이므로 프론트 엔드를 소개하지 않습니다. 직접 온라인으로 정보를 확인할 수 있습니다. 마지막으로, 이전에 일부 연구 자료를 검색했을 때 동료의 작문 스타일이 기사와 거의 동일하다는 것을 알았습니다. 동료 가이 기사를 참조해야한다고 생각하므로 아래에 나열되어 참조 자료로 간주됩니다.