From 74ea32f3a07d93922d5a35d37df51a2c27082aad Mon Sep 17 00:00:00 2001 From: yiyan Date: Sat, 1 Jun 2019 19:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 21 +++-- .../java/com/ifish/quartz/ExecuteJob.java | 94 +++++++++++++++++-- .../java/com/ifish/quartz/ScheduleJob.java | 18 +--- .../ifish/socketNew/MinaServerHandler.java | 4 +- .../java/com/ifish/socketNew/SomeServer.java | 84 ++++++++++++++++- .../com/ifish/util/QueueMessageJsonUtil.java | 11 --- src/main/resources/application-context.xml | 49 ++++++---- src/main/resources/mq.properties | 6 +- 8 files changed, 224 insertions(+), 63 deletions(-) delete mode 100644 src/main/java/com/ifish/util/QueueMessageJsonUtil.java diff --git a/pom.xml b/pom.xml index d7e9664..9adee3c 100644 --- a/pom.xml +++ b/pom.xml @@ -22,12 +22,11 @@ test - - + org.quartz-scheduler @@ -125,9 +124,15 @@ - com.alibaba - fastjson - 1.2.56 + com.alibaba + fastjson + 1.2.56 + + + + org.apache.commons + commons-pool2 + 2.6.2 @@ -174,8 +179,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/com/ifish/quartz/ExecuteJob.java b/src/main/java/com/ifish/quartz/ExecuteJob.java index 7c8a12a..c71d949 100644 --- a/src/main/java/com/ifish/quartz/ExecuteJob.java +++ b/src/main/java/com/ifish/quartz/ExecuteJob.java @@ -1,8 +1,10 @@ package com.ifish.quartz; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.ifish.entity.*; -import com.ifish.enums.BooleanEnum; -import com.ifish.enums.NeteaseEnum; +import com.ifish.entity.event.QueueEventBody; +import com.ifish.entity.event.QueueEventEntity; import com.ifish.enums.PushTypeEnum; import com.ifish.netease.NeteaseIM; import com.ifish.service.DeviceService; @@ -16,11 +18,12 @@ import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; -import java.util.Date; -import java.util.HashMap; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Session; import java.util.List; -import java.util.Map; public class ExecuteJob implements Job { @@ -31,8 +34,86 @@ public class ExecuteJob implements Job { private UserService userService; @Autowired private DeviceService deviceService; + @Autowired + private JmsTemplate jmsTemplate; + @Autowired + private Destination ifish7PushQueueDestination; public void execute(JobExecutionContext context) throws JobExecutionException { + //任务传递的参数 + JobDataMap dataMap = context.getJobDetail().getJobDataMap(); + String macAddress = dataMap.getString("macAddress"); + String timestamp = dataMap.getString("timestamp"); + try { + log.info("离线时间 --> " + timestamp); + log.info("离线设备 --> " + macAddress); + //移除为null的连接 + IoSession session_cz = SomeServer.sessions_cz.get(macAddress); + if(session_cz==null || session_cz.getRemoteAddress()==null){ + SomeServer.sessions_cz.remove(macAddress); + } + //设备是否存在 + Device device = deviceService.getUniqueByProperty("macAddress", macAddress); + if(device!=null){ + //保存离线的设备信息 + DeviceOffline deviceOffline = new DeviceOffline(); + deviceOffline.setOfflineMacAddress(device.getMacAddress()); + deviceOffline.setOfflineFactoryCode(device.getFactoryCode()); + deviceOffline.setOfflineDeviceType(device.getHardwareType()); + deviceOffline.setOfflineSdkVersion(String.valueOf(device.getSdkVersion())); + deviceOffline.setOfflineTime(IfishUtil.StrToDate1(timestamp)); + deviceService.saveDeviceOffline(deviceOffline); + log.info("离线设备信息保存++++++++++++++++++++++++++++++++++++++++++"); + List list = deviceService.getListByProperty(device.getDeviceId()); + //是否被绑定 + if(list!=null){ + for (DeviceUser deviceUser : list) { + Integer userId = deviceUser.getPriId().getUserId(); + String showName = deviceUser.getShowName(); + String title = PushTypeEnum.offline_push.getValue(); + String msg = "【"+title+"】你的水族箱“"+showName+"”于"+timestamp+"离线,请及时查看!"; + User user = userService.findById(userId); + if(user!=null){ + //推送记录 + PushList pushList = new PushList(); + //记录推送 + pushList.setUserId(userId); + pushList.setDeviceId(deviceUser.getPriId().getDeviceId()); + pushList.setPhoneType("ALL"); + pushList.setShowName(showName); + pushList.setPushType(PushTypeEnum.offline_push.getKey()); + pushList.setPushTitle(title); + pushList.setPushContext(msg); + pushList.setNumber1(device.getNumber1()); + pushList.setNumber2(device.getNumber2()); + pushList.setNumber3(device.getNumber3()); + pushList.setNumber4(device.getNumber4()); + pushList.setNumber5(device.getNumber5()); + JSONObject data = JSON.parseObject(JSON.toJSONString(pushList)); + QueueEventEntity eventEntity = new QueueEventEntity(); + eventEntity.setEventName("com.ifish7.mq.queues.event.PushNotifcationEvent"); + eventEntity.setEventProcess("deviceNotifcationPlus"); + QueueEventBody eventBody = new QueueEventBody("com.ifish7.mq.business.user.entity.TblPushList",data); + eventEntity.setEventBody(eventBody); + //推送至消息推送队列 + sendPushQueueMessage(JSONObject.toJSONString(eventEntity)); + } + } + } + } + } catch (Exception e) { + log.error(e.getMessage(),e); + } + } + + /** + * 推送Push消息队列 + * @param json 内容 + */ + private void sendPushQueueMessage(final String json) throws JMSException { + jmsTemplate.send(ifish7PushQueueDestination,(Session session) -> session.createTextMessage(json)); + } + /*public void execute(JobExecutionContext context) throws JobExecutionException { //任务传递的参数 JobDataMap dataMap = context.getJobDetail().getJobDataMap(); String macAddress = dataMap.getString("macAddress"); @@ -113,5 +194,6 @@ public class ExecuteJob implements Job { } catch (Exception e) { log.error(e.getMessage(),e); } - } + }*/ + } \ No newline at end of file diff --git a/src/main/java/com/ifish/quartz/ScheduleJob.java b/src/main/java/com/ifish/quartz/ScheduleJob.java index b1a99eb..4865489 100644 --- a/src/main/java/com/ifish/quartz/ScheduleJob.java +++ b/src/main/java/com/ifish/quartz/ScheduleJob.java @@ -1,15 +1,7 @@ package com.ifish.quartz; import org.apache.log4j.Logger; -import org.quartz.JobBuilder; -import org.quartz.JobDetail; -import org.quartz.JobKey; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SchedulerFactory; -import org.quartz.Trigger; -import org.quartz.TriggerBuilder; -import org.quartz.TriggerKey; +import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -35,7 +27,7 @@ public class ScheduleJob { scheduler.setJobFactory(jobFactory); scheduler.start(); } catch (SchedulerException e) { - log.error(String.format("start scheduler error:{%s}",e.toString())); + log.info(String.format("start scheduler error:{%s}",e.toString())); } } /** @@ -60,7 +52,7 @@ public class ScheduleJob { //添加触发器 scheduler.scheduleJob(jobDetail,trigger); } catch (SchedulerException e) { - log.error(String.format("addJob error【macAddress:{%s},jobGroup:{%s},triggerGroup:{%s},errMsg:{%s}】",jobGroup.getMacAddress(),jobGroup.getJobName(),jobGroup.getTriggerGroup(),e.toString())); + log.info(String.format("addJob error【macAddress:{%s},jobGroup:{%s},triggerGroup:{%s},errMsg:{%s}】",jobGroup.getMacAddress(),jobGroup.getJobName(),jobGroup.getTriggerGroup(),e.toString())); // log.error("addJob error【macAddress:{},jobGroup:{},triggerGroup:{},errMsg:{}】",jobGroup.getMacAddress(),jobGroup.getJobName(),jobGroup.getTriggerGroup(),e.toString()); } } @@ -83,7 +75,7 @@ public class ScheduleJob { //删除任务 scheduler.deleteJob(jobKey); } catch (SchedulerException e) { - log.error(String.format("deleteJob error【macAddress:{%s},jobGroup:{%s},triggerGroup:{%s},errMsg:{%s}】",jobGroup.getMacAddress(),jobGroup.getJobName(),jobGroup.getTriggerGroup(),e.toString())); + log.info(String.format("deleteJob error【macAddress:{%s},jobGroup:{%s},triggerGroup:{%s},errMsg:{%s}】",jobGroup.getMacAddress(),jobGroup.getJobName(),jobGroup.getTriggerGroup(),e.toString())); } } @@ -95,7 +87,7 @@ public class ScheduleJob { //关闭任务 scheduler.shutdown(); } catch (SchedulerException e) { - log.error(String.format("shutdown scheduler error:{%s}",e.toString())); + log.info(String.format("shutdown scheduler error:{%s}",e.toString())); } } diff --git a/src/main/java/com/ifish/socketNew/MinaServerHandler.java b/src/main/java/com/ifish/socketNew/MinaServerHandler.java index d3e9cc7..c200fda 100644 --- a/src/main/java/com/ifish/socketNew/MinaServerHandler.java +++ b/src/main/java/com/ifish/socketNew/MinaServerHandler.java @@ -90,8 +90,8 @@ public class MinaServerHandler extends IoHandlerAdapter { //离线时间 jobGroup.setTimestamp(IfishUtil.format(new Date())); //10分钟后推送 update 30分钟 - jobGroup.setStartTime(new Date(System.currentTimeMillis() + 600000L * 3)); -// jobGroup.setStartTime(new Date(System.currentTimeMillis() + 6000)); +// jobGroup.setStartTime(new Date(System.currentTimeMillis() + 600000L * 3)); + jobGroup.setStartTime(new Date(System.currentTimeMillis() + 60000L)); scheduleJob.addJob(jobGroup); } diff --git a/src/main/java/com/ifish/socketNew/SomeServer.java b/src/main/java/com/ifish/socketNew/SomeServer.java index e3a3d96..ca38df1 100644 --- a/src/main/java/com/ifish/socketNew/SomeServer.java +++ b/src/main/java/com/ifish/socketNew/SomeServer.java @@ -1,8 +1,11 @@ package com.ifish.socketNew; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.ifish.entity.*; +import com.ifish.entity.event.QueueEventBody; +import com.ifish.entity.event.QueueEventEntity; import com.ifish.enums.BooleanEnum; -import com.ifish.enums.NeteaseEnum; import com.ifish.enums.PushTypeEnum; import com.ifish.netease.NeteaseIM; import com.ifish.quartz.JobGroup; @@ -22,8 +25,16 @@ import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.jms.core.MessageCreator; -import java.util.*; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.Session; +import java.util.Calendar; +import java.util.Date; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @@ -41,6 +52,10 @@ public class SomeServer { private ScheduleJob scheduleJob; @Autowired private NeteaseIM neteaseIM; + @Autowired + private JmsTemplate jmsTemplate; + @Autowired + private Destination ifish7PushQueueDestination; //是否回复心跳 public static boolean isReplay = true; @@ -305,6 +320,12 @@ public class SomeServer { session.write(model); } } + /** + * 温度预警推送 + * @param strSrc + * @param title + * @param contont + */ /** * 温度预警推送 * @param strSrc @@ -312,6 +333,63 @@ public class SomeServer { * @param contont */ public void pushNotifcation(String strSrc,String title,String contont){ + try { + Device device = deviceService.getUniqueByProperty("macAddress", strSrc); + if(device!=null){ + Integer deviceId = device.getDeviceId(); + //是否开启预警 + String onOff = device.getOnOff(); + if(onOff!=null && onOff.equals(BooleanEnum.YES.getKey())){ + //绑定设备的用户 + List list = deviceService.getListByProperty(device.getDeviceId()); + for (DeviceUser deviceUser : list) { + Integer userId = deviceUser.getPriId().getUserId(); + String showName = deviceUser.getShowName(); + String timestamp = IfishUtil.format(new Date()); + String msg = "【"+title+"】你的水族箱“"+showName+"”在"+timestamp+contont; + User user = userService.findById(userId); + if(user!=null){ + //推送记录 + PushList pushList = new PushList(); + //记录推送 + pushList.setUserId(userId); + pushList.setDeviceId(deviceId); + pushList.setPhoneType("ALL"); + pushList.setShowName(showName); + pushList.setPushType(PushTypeEnum.wendu_warn.getKey()); + pushList.setPushTitle(title); + pushList.setPushContext(msg); + pushList.setNumber1(device.getNumber1()); + pushList.setNumber2(device.getNumber2()); + pushList.setNumber3(device.getNumber3()); + pushList.setNumber4(device.getNumber4()); + pushList.setNumber5(device.getNumber5()); + JSONObject data = JSON.parseObject(JSON.toJSONString(pushList)); + QueueEventEntity eventEntity = new QueueEventEntity(); + eventEntity.setEventName("com.ifish7.mq.queues.event.PushNotifcationEvent"); + eventEntity.setEventProcess("deviceNotifcationPlus"); + QueueEventBody eventBody = new QueueEventBody("com.ifish7.mq.business.user.entity.TblPushList",data); + eventEntity.setEventBody(eventBody); + //推送至消息推送队列 + sendPushQueueMessage(JSONObject.toJSONString(eventEntity)); + } + } + } + } + } catch (Exception e) { + log.error("pushNotifcation error:macAddress:{},error msg:{}",strSrc,e.toString()); + } + } + + + /** + * 推送Push消息队列 + * @param json 内容 + */ + private void sendPushQueueMessage(final String json) throws JMSException{ + jmsTemplate.send(ifish7PushQueueDestination,(Session session) -> session.createTextMessage(json)); + } + /*public void pushNotifcation(String strSrc,String title,String contont){ try { Device device = deviceService.getUniqueByProperty("macAddress", strSrc); if(device!=null){ @@ -374,5 +452,5 @@ public class SomeServer { } catch (Exception e) { log.error("pushNotifcation error:macAddress:{},error msg:{}",strSrc,e.toString()); } - } + }*/ } diff --git a/src/main/java/com/ifish/util/QueueMessageJsonUtil.java b/src/main/java/com/ifish/util/QueueMessageJsonUtil.java deleted file mode 100644 index 7ccd554..0000000 --- a/src/main/java/com/ifish/util/QueueMessageJsonUtil.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ifish.util; - -/** - * @author: yan.y - * @Description: json工具 - * @Date: Created in 16:46 2019-04-10 - * @Modified by: - */ -public class QueueMessageJsonUtil { - -} diff --git a/src/main/resources/application-context.xml b/src/main/resources/application-context.xml index 68936a8..40b9d1a 100644 --- a/src/main/resources/application-context.xml +++ b/src/main/resources/application-context.xml @@ -3,11 +3,14 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" + xmlns:amq="http://activemq.apache.org/schema/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-4.0.xsd" + http://www.springframework.org/schema/context/spring-context-4.0.xsd + http://activemq.apache.org/schema/core + http://activemq.apache.org/schema/core/activemq-core-5.14.1.xsd" default-lazy-init="true"> @@ -76,33 +79,43 @@ - - - + + + + + - + + + + + + + + + + + + + + + - ${queue_data_name} - --> + - ${queue_push_name} - --> + \ No newline at end of file diff --git a/src/main/resources/mq.properties b/src/main/resources/mq.properties index 2409198..a5dcb67 100644 --- a/src/main/resources/mq.properties +++ b/src/main/resources/mq.properties @@ -1,5 +1,7 @@ -broker_url=tcp://localhost:61616 +#broker_url=tcp://test.ifish7.com:61616 +broker_url=tcp://www.ifish7.com:61616 username=admin -password=admin +#password=admin +password=adminifish7 queue_data_name=ifishDataMq queue_push_name=ifishPushMq \ No newline at end of file