} catch (URISyntaxException e) {
e.printStackTrace();
}
final AmqpConnection conn = AmqpConnection.connect(options );
_logger.fine("connection queue");
conn.queue().execute(new Task() {
@Override
public void run() {
_logger.fine("connection running, setup callbacks");
conn.onTransportFailure(new Callback<Throwable>() {
@Override
public void onSuccess(Throwable value) {
_logger.fine("transportFailure Success? " + str(value));
conn.close();
}
@Override
public void onFailure(Throwable value) {
_logger.fine("transportFailure Trouble! " + str(value));
conn.close();
}
});
conn.onConnected(new Callback<Void>() {
@Override
public void onSuccess(Void value) {
_logger.fine("on connect Success! in container " + container);
final AmqpSession session = conn.createSession();
Target rqtarget = new Target();
rqtarget.setAddress("rq-tgt");
final AmqpSender sender = session.createSender(rqtarget, QoS.AT_LEAST_ONCE, "request-yyy");
Source rqsource = new Source();
rqsource.setAddress("rs-src");
sender.getEndpoint().setSource(rqsource);
Source rssource = new Source();
rssource.setAddress("rs-src");
final AmqpReceiver receiver = session.createReceiver(rssource , QoS.AT_LEAST_ONCE, 10, "response-yyy");
Target rstarget = new Target();
final String address = "rs-tgt";
rstarget.setAddress(address);
receiver.getEndpoint().setTarget(rstarget);
sender.onRemoteClose(new Callback<ErrorCondition>() {
@Override
public void onSuccess(ErrorCondition value) {
_logger.fine("sender remote close!" + str(value));
}
@Override
public void onFailure(Throwable value) {
_logger.fine("sender remote close Trouble!" + str(value));
conn.close();
}
});
receiver.onRemoteClose(new Callback<ErrorCondition>() {
@Override
public void onSuccess(ErrorCondition value) {
_logger.fine("receiver remote close!" + str(value));
}
@Override
public void onFailure(Throwable value) {
_logger.fine("receiver remote close Trouble!" + str(value));
conn.close();
}
});
final Task work = new Task() {
private AtomicInteger count = new AtomicInteger();
@Override
public void run() {
Message message = session.createTextMessage("hello world! " + String.valueOf(count.incrementAndGet()));
message.setAddress("amqp://joze/rq-src");
String reply_to = "amqp://" + container + "/" + address;
message.setReplyTo(reply_to);
message.setCorrelationId("correlator");
final MessageDelivery md = sender.send(message);
md.onRemoteStateChange(new Callback<DeliveryState>() {
@Override
public void onSuccess(DeliveryState value) {
_logger.fine("delivery remote state change! " + str(value) +
" local: "+ str(md.getLocalState()) +
" remote: " + str(md.getRemoteState()));
}
@Override
public void onFailure(Throwable value) {
_logger.fine("remote state change Trouble!" + str(value));
conn.close();
}
});
md.onSettle(new Callback<DeliveryState>() {
@Override
public void onSuccess(DeliveryState value) {
_logger.fine("delivery settled! " + str(value) +
" local: "+ str(md.getLocalState()) +
" remote: " + str(md.getRemoteState()));
_logger.fine("sender settle mode state " +
" local receiver " + str(sender.getEndpoint().getReceiverSettleMode()) +
" local sender " + str(sender.getEndpoint().getSenderSettleMode()) +
" remote receiver " + str(sender.getEndpoint().getRemoteReceiverSettleMode()) +
" remote sender " + str(sender.getEndpoint().getRemoteSenderSettleMode()) +
""
);
}
@Override
public void onFailure(Throwable value) {
_logger.fine("delivery sending Trouble!" + str(value));
conn.close();
}
});
}
};
receiver.setDeliveryListener(new AmqpDeliveryListener() {
@Override
public void onMessageDelivery(
MessageDelivery delivery) {
Message message = delivery.getMessage();
_logger.fine("incoming message delivery! " +
" local " + str(delivery.getLocalState()) +
" remote " + str(delivery.getRemoteState()) +
" message " + str(message.getBody()) +
"");
delivery.onSettle(new Callback<DeliveryState>() {
@Override
public void onSuccess(DeliveryState value) {
_logger.fine("incoming message settled! ");
int i = countdown.decrementAndGet();
if ( i > 0 ) {
_logger.fine("More work " + str(i));
work.run();
} else {
conn.queue().executeAfter(100, TimeUnit.MILLISECONDS, new Task() {
@Override
public void run() {
_logger.fine("stopping sender");
sender.close();
}
});
conn.queue().executeAfter(200, TimeUnit.MILLISECONDS, new Task() {
@Override
public void run() {
_logger.fine("stopping receiver");
receiver.close();
}
});
conn.queue().executeAfter(300, TimeUnit.MILLISECONDS, new Task() {
@Override
public void run() {
_logger.fine("stopping session");
session.close();
}
});
conn.queue().executeAfter(400, TimeUnit.MILLISECONDS, new Task() {
@Override
public void run() {
_logger.fine("stopping connection");
conn.close();