|
@ -1,5 +1,6 @@
|
|
|
package com.yihu.hos.remoteManage.service;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.common.constants.ContextAttributes;
|
|
|
import com.yihu.hos.interceptor.LocalContext;
|
|
|
import com.yihu.hos.services.ServiceShellEventService;
|
|
@ -15,6 +16,7 @@ import org.zbus.net.http.Message;
|
|
|
import java.io.IOException;
|
|
|
import java.net.InetAddress;
|
|
|
import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author HZY
|
|
@ -32,13 +34,16 @@ public class RemoteShellService {
|
|
|
private ZbusBroker zbusBroker;
|
|
|
private Consumer consumer;
|
|
|
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
/**
|
|
|
* 发送shell命令消息
|
|
|
*
|
|
|
* @param command
|
|
|
* @param disConnection
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean sendShell(String command,boolean disConnection){
|
|
|
public boolean sendShell(String command, boolean disConnection) {
|
|
|
InetAddress addr = null;
|
|
|
try {
|
|
|
addr = InetAddress.getLocalHost();
|
|
@ -57,11 +62,11 @@ public class RemoteShellService {
|
|
|
}
|
|
|
|
|
|
|
|
|
public void proxy(Message message, Consumer consumer) {
|
|
|
public void handle(Message message, Consumer consumer) throws IOException {
|
|
|
String bodyString = message.getBodyString();
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SHELL_RESPONSE,bodyString);
|
|
|
String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SHELL_RESPONSE + attachment, bodyString);
|
|
|
System.out.println("回调返回:" + bodyString);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void start() {
|
|
@ -71,16 +76,29 @@ public class RemoteShellService {
|
|
|
}
|
|
|
|
|
|
String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
|
|
|
if (attachment==null){
|
|
|
if (attachment == null) {
|
|
|
return;
|
|
|
}
|
|
|
consumer = new Consumer(zbusBroker, ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment);
|
|
|
|
|
|
try {
|
|
|
consumer.start(this::proxy);
|
|
|
} catch (IOException e) {
|
|
|
if (consumer != null) {
|
|
|
consumer = new Consumer(zbusBroker, ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment);
|
|
|
String messageBodyString = consumer.queryMQ().getBodyString();
|
|
|
Map<String,Object> message = objectMapper.readValue(messageBodyString,Map.class);
|
|
|
Integer consumerCount = (Integer) message.get("consumerCount");
|
|
|
|
|
|
if (consumerCount<1) {
|
|
|
consumer.start(this::handle);
|
|
|
}
|
|
|
} else {
|
|
|
consumer = new Consumer(zbusBroker, ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment);
|
|
|
consumer.start(this::handle);
|
|
|
}
|
|
|
|
|
|
System.out.println("start success~");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|