RemoteShellService.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.yihu.hos.remoteManage.service;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.hos.common.constants.ContextAttributes;
  4. import com.yihu.hos.interceptor.LocalContext;
  5. import com.yihu.hos.services.ServiceShellEventService;
  6. import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
  7. import com.yihu.hos.web.framework.model.bo.ServiceShell;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Service;
  11. import org.zbus.broker.ZbusBroker;
  12. import org.zbus.mq.Consumer;
  13. import org.zbus.net.http.Message;
  14. import java.io.IOException;
  15. import java.net.InetAddress;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. /**
  20. * @author HZY
  21. * @vsrsion 1.0
  22. * Created at 2017/1/5.
  23. */
  24. @Service("RemoteShellService")
  25. public class RemoteShellService {
  26. public static final String BEAN_ID = "RemoteShellService";
  27. @Value("${hos.zbus.url}")
  28. private String zbusUrl;
  29. @Autowired
  30. private ServiceShellEventService serviceShellEventService;
  31. private ZbusBroker zbusBroker;
  32. private Consumer consumer;
  33. private static Map<String,String> cunsumerNap = new HashMap<>();
  34. @Autowired
  35. private ObjectMapper objectMapper;
  36. /**
  37. * 发送shell命令消息
  38. *
  39. * @param command
  40. * @param disConnection
  41. * @return
  42. */
  43. public boolean sendShell(String command, boolean disConnection) {
  44. InetAddress addr = null;
  45. try {
  46. addr = InetAddress.getLocalHost();
  47. String ip = addr.getHostAddress();
  48. ServiceShell serviceShell = new ServiceShell();
  49. serviceShell.setCommand(command);
  50. serviceShell.setFromHost(ip);
  51. serviceShell.setUpdated(new Date());
  52. serviceShell.setDisconnect(disConnection);
  53. serviceShellEventService.serviceShellSend(serviceShell);
  54. return true;
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. return false;
  59. }
  60. public void handle(Message message, Consumer consumer) throws IOException {
  61. String bodyString = message.getBodyString();
  62. String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
  63. LocalContext.getContext().setAttachment(ContextAttributes.SHELL_RESPONSE + attachment, bodyString);
  64. System.out.println("回调返回:" + bodyString);
  65. }
  66. public void start() {
  67. if (zbusBroker == null) {
  68. System.out.println("zbusBroker is null");
  69. return;
  70. }
  71. String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
  72. if (attachment == null) {
  73. return;
  74. }
  75. try {
  76. if (!cunsumerNap.containsKey(ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment)){
  77. consumer = new Consumer(zbusBroker, ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment);
  78. consumer.start(this::handle);
  79. cunsumerNap.put(ServiceFlowConstant.SHELL_RESPONSE + "@" + attachment,attachment);
  80. }
  81. System.out.println("start success~");
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. @Autowired
  87. public void setZbusBroker(ZbusBroker zbusBroker) {
  88. this.zbusBroker = zbusBroker;
  89. }
  90. @Override
  91. protected void finalize() throws Throwable {
  92. consumer.close();
  93. super.finalize();
  94. }
  95. }