posts - 88, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          通過rest api管理activemq

          Posted on 2016-10-22 17:31 Milo的海域 閱讀(1441) 評論(0)  編輯  收藏 所屬分類: Java

          知道activemq現在已經支持了rest api, 但是官方對這部分的介紹一筆帶過 (http://activemq.apache.org/rest.html),


          通過google居然也沒搜到一些有用的, 比如像刪除一個destination, 都是問的多,然后沒下文. 于是花了一些心思研究了一下:


          首先通過rest api獲取當前版本所有已支持的協議

              http://172.30.43.206:8161/api/jolokia/list


          然后根據json輸出關于removeTopic, removeQueue的mbean實現通過rest api刪除destination的方法, 注意到用GET請求而不是POST,不然會報錯 (官網的例子里用的wget給的靈感, 開始用了POST老報錯)


          import org.apache.activemq.command.ActiveMQQueue;
          import org.apache.activemq.command.ActiveMQTopic;
          import org.apache.http.auth.AuthScope;
          import org.apache.http.auth.UsernamePasswordCredentials;
          import org.apache.http.impl.client.BasicCredentialsProvider;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.springframework.http.HttpEntity;
          import org.springframework.http.HttpHeaders;
          import org.springframework.http.HttpMethod;
          import org.springframework.http.MediaType;
          import org.springframework.http.ResponseEntity;
          import org.springframework.http.client.ClientHttpRequestFactory;
          import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
          import org.springframework.web.client.RestTemplate;

          import javax.jms.Destination;
          import javax.jms.JMSException;
          import java.util.Arrays;


          public class MessageQueueAdmin {
              
          private static final RestTemplate restTemplate = getRestTemplate("admin""admin");

              
          private static String brokerHost = "172.30.43.206";
              
          private static String adminConsolePort = "8161";
              
          private static String protocol = "http";

              
          public static void removeDestination(Destination destination) throws JMSException {
                  String destName, destType;
                  
          if (destination instanceof ActiveMQQueue) {
                      destName 
          = ((ActiveMQQueue) destination).getQueueName();
                      destType 
          = "Queue";
                  } 
          else {
                      destName 
          = ((ActiveMQTopic) destination).getTopicName();
                      destType 
          = "Topic";
                  }

                  
          // build urls
                  String url = String.format("%s://%s:%s/api/jolokia/exec/org.apache.activemq:" +
                          
          "brokerName=localhost,type=Broker/remove%s/%s", protocol, brokerHost, adminConsolePort, destType, destName);
                  System.out.println(url);
                  
          // do operation
                  HttpHeaders headers = new HttpHeaders();
                  headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
                  HttpEntity
          <String> entity = new HttpEntity<String>("parameters", headers);
                  ResponseEntity response 
          = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
                  System.out.println(response.getBody());
              }

              
          public static void main(String[] args) throws JMSException {
                  ActiveMQTopic topic 
          = new ActiveMQTopic("test-activemq-topic");
                  removeDestination(topic);
              }


              
          private static RestTemplate getRestTemplate(String user, String password) {
                  DefaultHttpClient httpClient 
          = new DefaultHttpClient();
                  BasicCredentialsProvider credentialsProvider 
          = new BasicCredentialsProvider();
                  credentialsProvider.setCredentials(AuthScope.ANY, 
          new UsernamePasswordCredentials(user, password));
                  httpClient.setCredentialsProvider(credentialsProvider);
                  ClientHttpRequestFactory rf 
          = new HttpComponentsClientHttpRequestFactory(httpClient);

                  
          return new RestTemplate(rf);
              }
          }

          其他的請求,應該都是類似jolokia的exec get request的格式:


          https://jolokia.org/reference/html/protocol.html#exec


          <base url>/exec/<mbean name>/<operation name>/<arg1>/<arg2>/.
          主站蜘蛛池模板: 新干县| 平遥县| 鄢陵县| 黄龙县| 舒兰市| 乐亭县| 临猗县| 咸丰县| 望城县| 横峰县| 连平县| 鄂托克前旗| 龙门县| 乌苏市| 西藏| 扶余县| 光泽县| 炎陵县| 钦州市| 虞城县| 青神县| 中西区| 九江县| 大名县| 金塔县| 雅江县| 盐亭县| 政和县| 紫云| 金阳县| 巢湖市| 台江县| 苏尼特右旗| 墨竹工卡县| 芦山县| 镇原县| 两当县| 朔州市| 稷山县| 淳安县| 苍梧县|