版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a919423654/article/details/86476184
@Override
public void sendDates() throws Exception {
String downLoadPath = Constant.wsUpload.get(Constant.WsAssetsKey.downLoadPath);
//获取资产文件
String uuid = UuidUtil.getUUID();
HttpUtil.postFile(downLoadPath,null,
downLoadPath,uuid+".xls");
File file = new File(downLoadPath
+Constant.Common.FILE_SEPARATOR
+uuid+".xls");
if(file.exists()){//如果文件存在,则进行上传
String fileName = downLoadPath +Constant.Common.FILE_SEPARATOR +uuid+".xls";
String zipName = downLoadPath+Constant.Common.FILE_SEPARATOR +uuid+".zip";
//对文件进行压缩
ZipUtils.zip(fileName,zipName);
//文件生成字节数组
byte[] uploadByte = FileUtil.fileToByteArray(zipName);
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//
EndpointReference targetEPR = new EndpointReference("http://www.127.0.0.1:8080/mytest/app/services/receiveDeviceStatus");
options.setTo(targetEPR);
//有的系统 设置setTimeOutInMilliSeconds 不起作用,还是都写上比较好
options.setTimeOutInMilliSeconds(1000*60*4);//毫秒 超时时间 4分钟
options.setProperty(HTTPConstants.SO_TIMEOUT, 1000*60*10);//4分钟
//参数
Object[] opAddEntryArgs = new Object[]{uuid,uploadByte};
//返回值类型
Class<?>[] classes = new Class<?>[]{ String.class };
QName opAddEntry = new QName("http://impl.service.ws.topsec.com","receive");
Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
String uploadResult = returnValue.toString();
}else{
logger.error("获取资产失败");
}
}
接收方
/**
*
* @param dataByte 数据文件
* @param fileName 文件名称, 名称规则 单位编号()+数据数量+时间(精确到毫秒)+8位的随机数 , 没有后缀
* @return
*/
@Override
@MTOM //服务的标志MTOM协议发布
public String receive(String fileName,byte[] dataByte) {
//fileName.substring(0,fileName.lastIndexOf("."));
Long receiveStartTime = System.currentTimeMillis();
try {
//String dateJsonStr = processDatas(new String(dataByte,"utf-8"));
//数据转换文件
processDatesFile(fileName,dataByte);
//读取文件里面的数据
List<String> dataStrList = readFileToData(
Constant.wsUpload.get(Constant.WsEventKey.receiveTempPath)
+Constant.Common.FILE_SEPARATOR
+fileName+Constant.Common.FILE_SEPARATOR+fileName+".txt");
String myZone = Constant.serverInfo.get(Constant.ServerInfo.ZONE);
List<Map<String,Object>> datas = dataToEventPojo(myZone,dataStrList);
//AGT_DOMAIN_ID
for(Map<String,Object> map : datas){
//区域
String dataZone = (String) map.get("AGT_DOMAIN_ID");
//多级入库时间
Date DVC_RECEIPT_TIME = new Date();
if(StringUtils.isBlank(dataZone)){
map.put("AGT_DOMAIN_ID","/"+ myZone+dataZone);
map.put("DVC_RECEIPT_TIME",DVC_RECEIPT_TIME);
}
}
/* */
//List<EventPojo> datas = JSONArray.parseArray(dataStrList,EventPojo.class);
try {
//根据用户名,密码,url创建一个连接工厂
ActiveMQConnectionFactory factory =
new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//从工厂中获取一个连接
Connection connection = factory.createConnection();
connection.start();
//创建一个session
//第一个参数:是否支持事务,如果为true,则会忽略第二个参数,被jms服务器设置为SESSION_TRANSACTED
//第二个参数为false时,paramB的值可为Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE其中一个。
//Session.AUTO_ACKNOWLEDGE为自动确认,客户端发送和接收消息不需要做额外的工作。哪怕是接收端发生异常,也会被当作正常发送成功。
//Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后,必须调用javax.jms.Message的acknowledge方法。jms服务器才会当作发送成功,并删除消息。
//DUPS_OK_ACKNOWLEDGE允许副本的确认模式。一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认消息的接收;而且允许重复确认。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//目的地,其实就是连接到哪个队列,如果是点对点,那么它的实现是Queue,如果是订阅模式,那它的实现是Topic
//Destination destination = session.createQueue("text-msg");
Destination destination = session.createTopic("com.topsec.tsm.topic.rawevent");
//从session中,获取一个消息生产者
MessageProducer producer = session.createProducer(destination);
//设置生产者的模式,有两种可选
//DeliveryMode.PERSISTENT 当activemq关闭的时候,队列数据将会被保存
//DeliveryMode.NON_PERSISTENT 当activemq关闭的时候,队列里面的数据将会被清空
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//创建一条消息,当然,消息的类型有很多,如文字,字节,对象等,可以通过session.create..方法来创建出来
//for(EventPojo data : datas) {
ObjectMessage message = session.createObjectMessage((Serializable) datas);
producer.send(message);
//}
Long nowTime = System.currentTimeMillis();
producer.close();
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
return "fail";
}
System.out.println("接收完成");
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
如果数据量比较大 需要 添加 @MTOM 协议