【发布时间】: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