@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 協議