RemoteShellService.java 3.3 KB

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