use of org.apache.activemq.artemis.rest.queue.push.xml.XmlHttpHeader in project activemq-artemis by apache.
the class UriStrategy method push.
@Override
public boolean push(ClientMessage message) {
ActiveMQRestLogger.LOGGER.debug("Pushing " + message);
String uri = createUri(message);
for (int i = 0; i < registration.getMaxRetries(); i++) {
long wait = registration.getRetryWaitMillis();
System.out.println("Creating request from " + uri);
ClientRequest request = executor.createRequest(uri);
request.followRedirects(false);
ActiveMQRestLogger.LOGGER.debug("Created request " + request);
for (XmlHttpHeader header : registration.getHeaders()) {
ActiveMQRestLogger.LOGGER.debug("Setting XmlHttpHeader: " + header.getName() + "=" + header.getValue());
request.header(header.getName(), header.getValue());
}
HttpMessageHelper.buildMessage(message, request, contentType, jmsOptions);
ClientResponse<?> res = null;
try {
ActiveMQRestLogger.LOGGER.debug(method + " " + uri);
res = request.httpMethod(method);
int status = res.getStatus();
ActiveMQRestLogger.LOGGER.debug("Status of push: " + status);
if (status == 503) {
String retryAfter = res.getStringHeaders().getFirst("Retry-After");
if (retryAfter != null) {
wait = Long.parseLong(retryAfter) * 1000;
}
} else if (status == 307) {
uri = res.getLocation().toString();
wait = 0;
} else if ((status >= 200 && status < 299) || status == 303 || status == 304) {
ActiveMQRestLogger.LOGGER.debug("Success");
return true;
} else if (status >= 400) {
switch(status) {
// these usually mean the message you are trying to send is crap, let dead letter logic take over
case 400:
case 411:
case 412:
case 413:
case 414:
case 415:
case 416:
throw new RuntimeException("Something is wrong with the message, status returned: " + status + " for push registration of URI: " + uri);
// might as well consider these critical failures and abort. Immediately signal to disable push registration depending on config
case 401:
case 402:
case 403:
case 405:
case 406:
case 407:
case 417:
case 505:
return false;
// request timeout, gone, and not found treat as a retry
case 404:
case 408:
case 409:
case 410:
break;
default:
// all 50x requests just retry (except 505)
break;
}
}
} catch (Exception e) {
ActiveMQRestLogger.LOGGER.failedToPushMessageToUri(uri, e);
return false;
} finally {
if (res != null)
res.releaseConnection();
}
try {
if (wait > 0)
Thread.sleep(wait);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted");
}
}
return false;
}
use of org.apache.activemq.artemis.rest.queue.push.xml.XmlHttpHeader in project activemq-artemis by apache.
the class ActiveMQPushStrategy method initialize.
protected void initialize() throws Exception {
super.start();
initialized = true;
initAuthentication();
ClientRequest request = executor.createRequest(registration.getTarget().getHref());
for (XmlHttpHeader header : registration.getHeaders()) {
request.header(header.getName(), header.getValue());
}
ClientResponse<?> res = request.head();
if (res.getStatus() != 200) {
throw new RuntimeException("Failed to query REST destination for init information. Status: " + res.getStatus());
}
String url = (String) res.getHeaders().getFirst("msg-create-with-id");
if (url == null) {
if (res.getLinkHeader() == null) {
throw new RuntimeException("Could not find create-with-id URL");
}
Link link = res.getLinkHeader().getLinkByTitle("create-with-id");
if (link == null) {
throw new RuntimeException("Could not find create-with-id URL");
}
url = link.getHref();
}
targetUri = ResteasyUriBuilder.fromTemplate(url);
}
Aggregations