123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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;
- import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
- import com.yihu.hos.web.framework.model.bo.ServiceShell;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.zbus.broker.ZbusBroker;
- import org.zbus.mq.Consumer;
- import org.zbus.net.http.Message;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author HZY
- * @vsrsion 1.0
- * Created at 2017/1/5.
- */
- @Service("RemoteShellService")
- public class RemoteShellService {
- public static final String BEAN_ID = "RemoteShellService";
- @Value("${hos.zbus.url}")
- private String zbusUrl;
- @Autowired
- private ServiceShellEventService serviceShellEventService;
- private ZbusBroker zbusBroker;
- private Consumer consumer;
- private static Map<String,String> cunsumerNap = new HashMap<>();
- @Autowired
- private ObjectMapper objectMapper;
- /**
- * 发送shell命令消息
- *
- * @param command
- * @param disConnection
- * @return
- */
- public boolean sendShell(String command, boolean disConnection) {
- InetAddress addr = null;
- try {
- addr = InetAddress.getLocalHost();
- String ip = addr.getHostAddress();
- ServiceShell serviceShell = new ServiceShell();
- serviceShell.setCommand(command);
- serviceShell.setFromHost(ip);
- serviceShell.setUpdated(new Date());
- serviceShell.setDisconnect(disConnection);
- serviceShellEventService.serviceShellSend(serviceShell);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public void handle(Message message, Consumer consumer) throws IOException {
- String bodyString = message.getBodyString();
- String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
- LocalContext.getContext().setAttachment(ContextAttributes.SHELL_RESPONSE + attachment, bodyString);
- System.out.println("回调返回:" + bodyString);
- }
- public void start() {
- if (zbusBroker == null) {
- System.out.println("zbusBroker is null");
- return;
- }
- String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
- if (attachment == null) {
- return;
- }
- try {
- if (!cunsumerNap.containsKey(ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment)){
- consumer = new Consumer(zbusBroker, ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment);
- consumer.start(this::handle);
- cunsumerNap.put(ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment,attachment);
- }
- System.out.println("start success~");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Autowired
- public void setZbusBroker(ZbusBroker zbusBroker) {
- this.zbusBroker = zbusBroker;
- }
- @Override
- protected void finalize() throws Throwable {
- consumer.close();
- super.finalize();
- }
- }
|