axis2 傳送
阿新 • • 發佈:2019-01-14
@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 協議