【问题标题】:How to create Activemq http listener using java如何使用 java 创建 Activemq http 监听器
【发布时间】:2016-11-29 21:01:46
【问题描述】:

我使用以下代码来读取 activemq 消息。我需要使用 REST 实现 activemq 侦听器。

这是消息阅读演示类

public class ActiveMQConsumer {

    public static void main(String args[]) {
         String url = "http://localhost:8161/api/message?destination=queue://test.queue&readTimeout=10000&type=queue&clientId=test";

         RestClient client = new RestClient(userName, password);
         String activeMQResponse = client.get(url);
         System.out.println(activeMQResponse);
     }
}

这是spring resttemplate HTTP连接类

public class RestClient {

    private String server = "";
    private RestTemplate rest;
    private HttpHeaders headers;
    private HttpStatus status;

    public RestClient(String userName, String password) {
        this.rest = new RestTemplate();
        this.headers = createHeaders(userName, password);
    }

    public HttpHeaders createHeaders(String username, String password) {
        return new HttpHeaders() {
            {
                String auth = username + ":" + password;
                byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
                String authHeader = "Basic " + new String(encodedAuth);
                set("Authorization", authHeader);
                set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                set("Accept", "*/*");
            }
        };
    }

    public String get(String uri) {
        HttpEntity<String> requestEntity = new HttpEntity<String>("", headers);
        ResponseEntity<String> responseEntity = rest.exchange(server + uri, HttpMethod.GET, requestEntity, String.class);
        this.setStatus(responseEntity.getStatusCode());
        return responseEntity.getBody();
    }   

    public HttpStatus getStatus() {
        return status;
    }

    public void setStatus(HttpStatus status) {
        this.status = status;
    }
}

【问题讨论】:

    标签: java spring activemq resttemplate


    【解决方案1】:

    首先,我不会使用 RestClient 来联系一些代理,因为它不仅仅是简单的 http 通信。它是mqttamqt 协议而不是http。好的,我看到 active mq 提供了 http api,但我仍然建议采用不同的方法。

    使用apache-camelactivemq 组件。然后 您只需创建如下路线:

    from("activemq:queue:your_queue")
      .process(e -> {...}) // your processing of message goes here.
    

    更多关于如何创建路由的信息可以在here找到。好消息是,camel 有很好的支持和很好的文档。

    如果要求您必须从休息中消费消息,那么我可能会使用unirest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-27
      • 2015-02-25
      • 2011-10-19
      • 2011-10-19
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多