簡體   English   中英

如何使用Java創建Activemq http監聽器

[英]How to create Activemq http listener using java

我已經使用以下代碼讀取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;
    }
}

首先,我不會使用RestClient與某些經紀人聯系,因為它不僅是簡單的http通信。 它是mqttamqt協議,而不是http 好的,我看到活動的mq提供了http api,但是我還是建議采用其他方法。

使用apache-camel activemq組件。 然后,您只需創建一條路線,例如:

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

有關如何創建路線的更多信息,請參見此處 好消息是,駱駝有很好的支持,也有很好的文檔。

如果需要,您必須從休息中消費消息,那么我可能會使用unirest

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM