Преглед изворни кода

Merge branch 'fix/consolebug' into 'develop'

Fix/consolebug  修复包分发多台服务器有一个bug

See merge request o2oa/o2oa!568
o2null пре 5 година
родитељ
комит
a53d417695

+ 173 - 155
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/command/ActionUploadFile.java

@@ -1,155 +1,173 @@
-package com.x.program.center.jaxrs.command;
-
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.InputStream;
-import java.net.Socket;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
-import com.x.base.core.project.annotation.FieldDescribe;
-import com.x.base.core.project.config.Config;
-import com.x.base.core.project.config.Nodes;
-import com.x.base.core.project.gson.GsonPropertyObject;
-import com.x.base.core.project.gson.XGsonBuilder;
-import com.x.base.core.project.http.ActionResult;
-import com.x.base.core.project.http.EffectivePerson;
-import com.x.base.core.project.logger.Logger;
-import com.x.base.core.project.logger.LoggerFactory;
-import com.x.base.core.project.tools.Crypto;
-
-
-public class ActionUploadFile  extends BaseAction {
-    private static Logger logger = LoggerFactory.getLogger(CommandAction.class);
-
-	ActionResult<Wo> execute(EffectivePerson effectivePerson, String ctl, String nodeName , String nodePort, InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception {
-			ActionResult<Wo> result = new ActionResult<>();	
-			Wo wo  = null;
-			if(nodeName.equalsIgnoreCase("*")) {
-				Nodes nodes = Config.nodes();
-				for (String node : nodes.keySet()){
-					if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
-				      wo = executeCommand( ctl,  node ,  nodes.get(node).nodeAgentPort(),  fileInputStream, disposition);
-					}
-				}
-			}else {
-				
-			     wo = executeCommand( ctl,  nodeName ,  Integer.parseInt(nodePort),  fileInputStream, disposition);
-			}
-			result.setData(wo);
-			return result;
-	}
-
-	synchronized private Wo executeCommand(String ctl , String nodeName ,int nodePort,InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception{
-		Wo wo = new Wo();
-		wo.setNode(nodeName);
-		wo.setStatus("success");
-		try (Socket socket = new Socket(nodeName, nodePort)) {
-			socket.setKeepAlive(true);
-			socket.setSoTimeout(5000);
-			DataOutputStream dos = null;
-			DataInputStream dis  = null;
-			try {
-				dos = new DataOutputStream(socket.getOutputStream());
-			    dis = new DataInputStream(socket.getInputStream());
-			    
-				Map<String, Object> commandObject = new HashMap<>();
-				commandObject.put("command", "redeploy:"+ ctl);
-				commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
-				dos.writeUTF(XGsonBuilder.toJson(commandObject));
-				dos.flush();
-				
-				dos.writeUTF(disposition.getFileName());
-				dos.flush();
-				
-				logger.info("发送文件starting.......");
-				byte[] bytes = new byte[1024];
-				int length =0;
-				while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
-					dos.write(bytes, 0, length);
-					dos.flush();
-				}
-				logger.info("发送文件end.......");
-				
-			}finally {
-				dos.close();
-				dis.close();
-				fileInputStream.close();
-			}
-		} catch (Exception ex) {
-			wo.setStatus("fail");
-			//logger.warn("socket dispatch executeCommand to {}:{} error={}", nodeName, nodePort, ex.getMessage());
-		}
-        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-		wo.setTime(df.format(new Date()));
-		return wo;
-	}
-	
-
-	public static class Wi  extends GsonPropertyObject{
-		private String ctl;
-		private String nodeName;
-		private String nodePort;
-		
-		public String getCtl() {
-			return ctl;
-		}
-		public void setCtl(String ctl) {
-			this.ctl = ctl;
-		}
-		public String getNodeName() {
-			return nodeName;
-		}
-		public void setNodeName(String nodeName) {
-			this.nodeName = nodeName;
-		}
-		public String getNodePort() {
-			return nodePort;
-		}
-		public void setNodePort(String nodePort) {
-			this.nodePort = nodePort;
-		}
-	}
-	
-	public static class Wo extends GsonPropertyObject {
-		
-		@FieldDescribe("执行时间")
-		private String time;
-		@FieldDescribe("执行结束")
-		private String status;
-		@FieldDescribe("执行服务器")
-		private String node;
-
-		public String getTime() {
-			return time;
-		}
-		
-		public void setTime(String time) {
-			this.time = time;
-		}
-		
-		public String getNode() {
-			return node;
-		}
-
-		public void setNode(String node) {
-			this.node = node;
-		}
-
-		public String getStatus() {
-			return status;
-		}
-
-		public void setStatus(String status) {
-			this.status = status;
-		}
-	}
-	
-}
-
-
-
+package com.x.program.center.jaxrs.command;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.InputStream;
+import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+
+import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
+
+import com.hankcs.hanlp.corpus.io.IOUtil;
+import com.x.base.core.project.annotation.FieldDescribe;
+import com.x.base.core.project.config.Config;
+import com.x.base.core.project.config.Nodes;
+import com.x.base.core.project.gson.GsonPropertyObject;
+import com.x.base.core.project.gson.XGsonBuilder;
+import com.x.base.core.project.http.ActionResult;
+import com.x.base.core.project.http.EffectivePerson;
+import com.x.base.core.project.logger.Logger;
+import com.x.base.core.project.logger.LoggerFactory;
+import com.x.base.core.project.tools.Crypto;
+
+
+public class ActionUploadFile  extends BaseAction {
+    private static Logger logger = LoggerFactory.getLogger(CommandAction.class);
+
+	ActionResult<Wo> execute(EffectivePerson effectivePerson,HttpServletRequest request, String ctl, String nodeName , String nodePort, InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception {
+			ActionResult<Wo> result = new ActionResult<>();	
+			Wo wo  = null;
+			String curServer = request.getLocalAddr();
+			ByteArrayInputStream byteArrayInputStream = null;
+			byte[] byteArray = IOUtil.readBytesFromOtherInputStream(fileInputStream);
+			fileInputStream.close();
+			if(nodeName.equalsIgnoreCase("*")) {
+				Nodes nodes = Config.nodes();
+				for (String node : nodes.keySet()){
+					//先其他服务器
+					if(!node.equalsIgnoreCase(curServer)) {
+						if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
+							 byteArrayInputStream = new ByteArrayInputStream(byteArray);
+					      wo = executeCommand( ctl,  node ,  nodes.get(node).nodeAgentPort(),  byteArrayInputStream, disposition);
+						}
+					}
+				   //后当前服务器
+				    if(nodes.get(curServer).getApplication().getEnable() || nodes.get(curServer).getCenter().getEnable()){
+				        	 byteArrayInputStream = new ByteArrayInputStream(byteArray);
+					      wo = executeCommand( ctl,  curServer ,  nodes.get(curServer).nodeAgentPort(),  byteArrayInputStream, disposition);
+			        }
+				}
+			}else {
+				
+			     wo = executeCommand( ctl,  nodeName ,  Integer.parseInt(nodePort),  fileInputStream, disposition);
+			}
+			
+			result.setData(wo);
+			return result;
+	}
+
+	synchronized private Wo executeCommand(String ctl , String nodeName ,int nodePort,InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception{
+		Wo wo = new Wo();
+		wo.setNode(nodeName);
+		wo.setStatus("success");
+		try (Socket socket = new Socket(nodeName, nodePort)) {
+			socket.setKeepAlive(true);
+			socket.setSoTimeout(5000);
+			DataOutputStream dos = null;
+			DataInputStream dis  = null;
+			try {
+				dos = new DataOutputStream(socket.getOutputStream());
+			    dis = new DataInputStream(socket.getInputStream());
+			    
+				Map<String, Object> commandObject = new HashMap<>();
+				commandObject.put("command", "redeploy:"+ ctl);
+				commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
+				dos.writeUTF(XGsonBuilder.toJson(commandObject));
+				dos.flush();
+				
+				dos.writeUTF(disposition.getFileName());
+				dos.flush();
+				
+				logger.info("发送文件starting.......");
+				byte[] bytes = new byte[1024];
+				int length =0;
+				while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
+					dos.write(bytes, 0, length);
+					dos.flush();
+				}
+				logger.info("发送文件end.......");
+				
+			}finally {
+				dos.close();
+				dis.close();
+				socket.close();
+				fileInputStream.close();
+			}
+		} catch (Exception ex) {
+			wo.setStatus("fail");
+			//logger.warn("socket dispatch executeCommand to {}:{} error={}", nodeName, nodePort, ex.getMessage());
+		}
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+		wo.setTime(df.format(new Date()));
+		return wo;
+	}
+	
+
+	public static class Wi  extends GsonPropertyObject{
+		private String ctl;
+		private String nodeName;
+		private String nodePort;
+		
+		public String getCtl() {
+			return ctl;
+		}
+		public void setCtl(String ctl) {
+			this.ctl = ctl;
+		}
+		public String getNodeName() {
+			return nodeName;
+		}
+		public void setNodeName(String nodeName) {
+			this.nodeName = nodeName;
+		}
+		public String getNodePort() {
+			return nodePort;
+		}
+		public void setNodePort(String nodePort) {
+			this.nodePort = nodePort;
+		}
+	}
+	
+	public static class Wo extends GsonPropertyObject {
+		
+		@FieldDescribe("执行时间")
+		private String time;
+		@FieldDescribe("执行结束")
+		private String status;
+		@FieldDescribe("执行服务器")
+		private String node;
+
+		public String getTime() {
+			return time;
+		}
+		
+		public void setTime(String time) {
+			this.time = time;
+		}
+		
+		public String getNode() {
+			return node;
+		}
+
+		public void setNode(String node) {
+			this.node = node;
+		}
+
+		public String getStatus() {
+			return status;
+		}
+
+		public void setStatus(String status) {
+			this.status = status;
+		}
+	}
+	
+}
+
+
+

+ 1 - 2
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/command/CommandAction.java

@@ -70,7 +70,6 @@ public class CommandAction<Wo> extends StandardJaxrsAction {
 		asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
 	}
 	
-
 	@JaxrsMethodDescribe(value = "上传customJar,customWar,storeJar,storeWar包并自动部署", action = ActionUploadFile.class)
 	@POST
 	@Path("upload")
@@ -86,7 +85,7 @@ public class CommandAction<Wo> extends StandardJaxrsAction {
 		ActionResult<ActionUploadFile.Wo> result = new ActionResult<>();
 		EffectivePerson effectivePerson = this.effectivePerson(request);
 		try {
-			result = new ActionUploadFile().execute(effectivePerson, ctl, nodeName, nodePort, fileInputStream, disposition);
+			result = new ActionUploadFile().execute(effectivePerson, request ,ctl, nodeName, nodePort, fileInputStream, disposition);
 		} catch (Exception e) {
 			logger.error(e, effectivePerson, request, null);
 			result.error(e);