通过rest api管理activemq

知道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>/.

你可能感兴趣的:(通过rest api管理activemq)