From c5dbae94d8e6ce01d9c6933e6a019a74eb861fed Mon Sep 17 00:00:00 2001 From: yiyan Date: Wed, 10 Apr 2019 17:33:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BF=AE=E6=94=B9=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 32 ++++- .../ifish/entity/event/QueueEventBody.java | 37 ++++++ .../ifish/entity/event/QueueEventEntity.java | 42 +++++++ .../java/com/ifish/socketNew/SomeServer.java | 83 +++++++++++-- .../com/ifish/util/QueueMessageJsonUtil.java | 11 ++ src/main/resources/application-context.xml | 38 +++++- src/main/resources/mq.properties | 5 + .../ifish/socketNew/SomeServerQueueTest.java | 75 ++++++++++++ src/test/resources/application-context.xml | 113 ++++++++++++++++++ src/test/resources/mq.properties | 5 + 10 files changed, 428 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/ifish/entity/event/QueueEventBody.java create mode 100644 src/main/java/com/ifish/entity/event/QueueEventEntity.java create mode 100644 src/main/java/com/ifish/util/QueueMessageJsonUtil.java create mode 100644 src/main/resources/mq.properties create mode 100644 src/test/java/com/ifish/socketNew/SomeServerQueueTest.java create mode 100644 src/test/resources/application-context.xml create mode 100644 src/test/resources/mq.properties diff --git a/pom.xml b/pom.xml index 74a0309..b917f3b 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,26 @@ http://mvnrepository.com + + junit + junit + 4.12 + test + + + org.springframework + spring-test + 4.1.6.RELEASE + test + + + + + org.apache.activemq + activemq-all + 5.15.4 + + org.quartz-scheduler quartz @@ -103,7 +123,13 @@ httpclient 4.3.5 - + + + com.alibaba + fastjson + 1.2.56 + + org.json json @@ -148,8 +174,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.6 - 1.6 + 1.8 + 1.8 diff --git a/src/main/java/com/ifish/entity/event/QueueEventBody.java b/src/main/java/com/ifish/entity/event/QueueEventBody.java new file mode 100644 index 0000000..ba037a5 --- /dev/null +++ b/src/main/java/com/ifish/entity/event/QueueEventBody.java @@ -0,0 +1,37 @@ +package com.ifish.entity.event; + +import com.alibaba.fastjson.JSONObject; + +/** + * @author: yan.y + * @Description: + * @Date: Created in 16:58 2019-04-10 + * @Modified by: + */ +public class QueueEventBody { + + public QueueEventBody(String entity, JSONObject data) { + this.entity = entity; + this.data = data; + } + + private String entity; + + private JSONObject data; + + public String getEntity() { + return entity; + } + + public void setEntity(String entity) { + this.entity = entity; + } + + public JSONObject getData() { + return data; + } + + public void setData(JSONObject data) { + this.data = data; + } +} diff --git a/src/main/java/com/ifish/entity/event/QueueEventEntity.java b/src/main/java/com/ifish/entity/event/QueueEventEntity.java new file mode 100644 index 0000000..6cda644 --- /dev/null +++ b/src/main/java/com/ifish/entity/event/QueueEventEntity.java @@ -0,0 +1,42 @@ +package com.ifish.entity.event; + +import com.alibaba.fastjson.JSONObject; + +/** + * @author: yan.y + * @Description: + * @Date: Created in 16:46 2019-04-10 + * @Modified by: + */ +public class QueueEventEntity { + + private String eventProcess; + + private String eventName; + + private QueueEventBody eventBody; + + public String getEventProcess() { + return eventProcess; + } + + public void setEventProcess(String eventProcess) { + this.eventProcess = eventProcess; + } + + public String getEventName() { + return eventName; + } + + public void setEventName(String eventName) { + this.eventName = eventName; + } + + public QueueEventBody getEventBody() { + return eventBody; + } + + public void setEventBody(QueueEventBody eventBody) { + this.eventBody = eventBody; + } +} diff --git a/src/main/java/com/ifish/socketNew/SomeServer.java b/src/main/java/com/ifish/socketNew/SomeServer.java index 345eabd..97d41d3 100644 --- a/src/main/java/com/ifish/socketNew/SomeServer.java +++ b/src/main/java/com/ifish/socketNew/SomeServer.java @@ -4,7 +4,10 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; +import com.alibaba.fastjson.JSONObject; import com.ifish.entity.*; +import com.ifish.entity.event.QueueEventBody; +import com.ifish.entity.event.QueueEventEntity; import com.ifish.socketNew.model.receive.*; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; @@ -29,6 +32,10 @@ import com.ifish.socketNew.model.send.OrderFunctionCode9; import com.ifish.socketNew.util.OrderModel; import com.ifish.util.ByteUtil; import com.ifish.util.IfishUtil; +import org.springframework.jms.core.JmsTemplate; + +import javax.jms.Destination; +import javax.jms.Session; /** @@ -44,6 +51,12 @@ public class SomeServer { private ScheduleJob scheduleJob; @Autowired private NeteaseIM neteaseIM; + @Autowired + private JmsTemplate jmsTemplate; + @Autowired + private Destination ifish7DataQueueDestination; + @Autowired + private Destination ifish7PushQueueDestination; //是否回复心跳 public static boolean isReplay = true; @@ -83,15 +96,31 @@ public class SomeServer { BackFunctionCodeHeater heater = (BackFunctionCodeHeater) message; String macAddress = ByteUtil.bytesToHexString(heater.getSrc()); String hour = String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); - List deviceHeaters = deviceService.getDeviceHeaterByProperty(hour, macAddress,IfishUtil.format1(new Date())); - if (deviceHeaters.size() > 0) { - DeviceHeater deviceHeater = deviceHeaters.get(0); - deviceHeater.setHeaterWaterTemperature(String.valueOf(heater.getWaterTemperature())); - deviceHeater.setHeaterPh(String.valueOf(heater.getPh())); - deviceService.update(deviceHeater); - } else { - deviceService.save(heater); - } +// List deviceHeaters = deviceService.getDeviceHeaterByProperty(hour, macAddress,IfishUtil.format1(new Date())); +// if (deviceHeaters.size() > 0) { +// DeviceHeater deviceHeater = deviceHeaters.get(0); +// deviceHeater.setHeaterWaterTemperature(String.valueOf(heater.getWaterTemperature())); +// deviceHeater.setHeaterPh(String.valueOf(heater.getPh())); +// deviceService.update(deviceHeater); +// } else { +// deviceService.save(heater); +// } + JSONObject data = new JSONObject(); + data.put("heaterMacAddress",macAddress); + data.put("heaterWaterTemperature",heater.getHeatingTemperature()); + data.put("heaterPh",heater.getPh()); + data.put("heaterGatheringDate",IfishUtil.format1(new Date())); + data.put("heaterGatheringTime",hour); + + QueueEventEntity eventEntity = new QueueEventEntity(); + eventEntity.setEventName("com.ifish7.mq.queues.event.IntelligentHeatingRodEvent"); + eventEntity.setEventProcess("intelligentHeatingRodSaveOrUpdate"); + QueueEventBody eventBody = new QueueEventBody("com.ifish7.mq.business.device.entity.TblDeviceHeater",data); + eventEntity.setEventBody(eventBody); + + //智能加热棒数据更新及保存 + sendDataQueueMessage(JSONObject.toJSONString(eventEntity)); + //设备重新连接上,则移除延时推送的任务 JobGroup jobGroup = new JobGroup(); jobGroup.setJobName(macAddress); @@ -151,6 +180,26 @@ public class SomeServer { jobGroup.setTriggerName(strSrc); scheduleJob.deleteJob(jobGroup); try { + //macAddr地址 + String stcMac = ByteUtil.bytesToHexString(model.getSrc()); + //IP + byte[] log_Ip= receive.getLogin_ip(); + StringBuilder ipStr = new StringBuilder(); + for (int i = 0; i < log_Ip.length; i++) { + int v = log_Ip[i] & 0xff; + if(i==0){ + ipStr.append(v); + } + else{ + ipStr.append("."+v); + } + } + Integer version = receive.getVersion() & 0xff; + String factoryCode = ByteUtil.toHex(receive.getVendor()); + factoryCode = factoryCode.equals("01")?"0a":factoryCode; + String typeCode = ByteUtil.toHex(receive.getHardware_type()); + + //更新设备信息 deviceService.update(receive); // if(device==null){ @@ -376,4 +425,20 @@ public class SomeServer { log.error("pushNotifcation error:macAddress:{},error msg:{}",strSrc,e.toString()); } } + + /** + * 推送Data消息队列 + * @param json 内容 + */ + private void sendDataQueueMessage(final String json){ + jmsTemplate.send(ifish7DataQueueDestination,(Session session) -> session.createTextMessage(json)); + } + + /** + * 推送Push消息队列 + * @param json 内容 + */ + private void sendPushQueueMessage(final String json) { + jmsTemplate.send(ifish7PushQueueDestination,(Session session) -> session.createTextMessage(json)); + } } diff --git a/src/main/java/com/ifish/util/QueueMessageJsonUtil.java b/src/main/java/com/ifish/util/QueueMessageJsonUtil.java new file mode 100644 index 0000000..7ccd554 --- /dev/null +++ b/src/main/java/com/ifish/util/QueueMessageJsonUtil.java @@ -0,0 +1,11 @@ +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 91fbaa4..f4d8376 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"> @@ -15,6 +18,7 @@ + @@ -74,4 +78,36 @@ ${netease.appSecret} + + + + + + + + + + + + + + + + + ${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 new file mode 100644 index 0000000..2409198 --- /dev/null +++ b/src/main/resources/mq.properties @@ -0,0 +1,5 @@ +broker_url=tcp://localhost:61616 +username=admin +password=admin +queue_data_name=ifishDataMq +queue_push_name=ifishPushMq \ No newline at end of file diff --git a/src/test/java/com/ifish/socketNew/SomeServerQueueTest.java b/src/test/java/com/ifish/socketNew/SomeServerQueueTest.java new file mode 100644 index 0000000..fa2e6c9 --- /dev/null +++ b/src/test/java/com/ifish/socketNew/SomeServerQueueTest.java @@ -0,0 +1,75 @@ +package com.ifish.socketNew; + +import com.alibaba.fastjson.JSONObject; +import com.ifish.entity.event.QueueEventBody; +import com.ifish.entity.event.QueueEventEntity; +import com.ifish.util.IfishUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.jms.Destination; +import javax.jms.Session; +import java.util.Date; + +/** + * @author: yan.y + * @Description: 测试队列消息发送 + * @Date: Created in 16:20 2019-04-10 + * @Modified by: + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations="classpath:application-context.xml") +public class SomeServerQueueTest { + @Autowired + private JmsTemplate jmsTemplate; + + @Autowired + private Destination ifish7DataQueueDestination; + @Autowired + private Destination ifish7PushQueueDestination; + + @Test + public void testDataMessageSend(){ + String json = "this's a test message"; + JSONObject data = new JSONObject(); + data.put("heaterMacAddress","12345"); + data.put("heaterWaterTemperature","130"); + data.put("heaterPh","1200"); + data.put("heaterGatheringDate", IfishUtil.format1(new Date())); + data.put("heaterGatheringTime","17"); + + QueueEventEntity eventEntity = new QueueEventEntity(); + eventEntity.setEventName("com.ifish7.mq.queues.event.IntelligentHeatingRodEvent"); + eventEntity.setEventProcess("intelligentHeatingRodSaveOrUpdate"); + QueueEventBody eventBody = new QueueEventBody("com.ifish7.mq.business.device.entity.TblDeviceHeater",data); + eventEntity.setEventBody(eventBody); + + String eventEntityStr = JSONObject.toJSONString(eventEntity); + jmsTemplate.send(ifish7DataQueueDestination,(Session session) -> session.createTextMessage(eventEntityStr)); + } + + @Test + public void testPushMessageSend(){ + String json = "this's a test message"; + JSONObject data = new JSONObject(); + data.put("heaterMacAddress","12345"); + data.put("heaterWaterTemperature","130"); + data.put("heaterPh","1200"); + data.put("heaterGatheringDate", IfishUtil.format1(new Date())); + data.put("heaterGatheringTime","17"); + + QueueEventEntity eventEntity = new QueueEventEntity(); + eventEntity.setEventName("com.ifish7.mq.queues.event.IntelligentHeatingRodEvent"); + eventEntity.setEventProcess("intelligentHeatingRodSaveOrUpdate"); + QueueEventBody eventBody = new QueueEventBody("com.ifish7.mq.business.device.entity.TblDeviceHeater",data); + eventEntity.setEventBody(eventBody); + + String eventEntityStr = JSONObject.toJSONString(eventEntity); + //智能加热棒数据更新及保存 + jmsTemplate.send(ifish7PushQueueDestination,(Session session) -> session.createTextMessage(eventEntityStr)); + } +} diff --git a/src/test/resources/application-context.xml b/src/test/resources/application-context.xml new file mode 100644 index 0000000..e1fd58f --- /dev/null +++ b/src/test/resources/application-context.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + classpath:/com/ifish/entity/hbm/*.hbm.xml + + + + + ${hibernate.dialect} + ${hibernate.show_sql} + ${hibernate.format_sql} + ${hibernate.hbm2ddl.auto} + ${hibernate.query.substitutions} + ${"hibernate.jdbc.batch_size"} + ${hibernate.cache.use_second_level_cache} + ${hibernate.cache.use_query_cache} + ${hibernate.cache.region.factory_class} + + + + + + + + + + + + + + + + + + ${netease.appKey} + + + + ${netease.appSecret} + + + + + + + + + + + + + + + + + + + ${queue_data_name} + + + + + + + ${queue_push_name} + + + + + \ No newline at end of file diff --git a/src/test/resources/mq.properties b/src/test/resources/mq.properties new file mode 100644 index 0000000..2409198 --- /dev/null +++ b/src/test/resources/mq.properties @@ -0,0 +1,5 @@ +broker_url=tcp://localhost:61616 +username=admin +password=admin +queue_data_name=ifishDataMq +queue_push_name=ifishPushMq \ No newline at end of file