use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class ReceiverTransactedAcquisition method main.
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("?")) {
System.out.println();
System.out.println("Usage: <host> <port> <source> <nmsgs> <qos> <txsize> <authanon> [<username> <password>]");
System.out.println(" <qos> ::= AT_LEAST_ONCE | AT_MOST_ONCE | EXACTLY_ONCE");
System.out.println(" Suppress <username> <password> and set <authanon> to false to avoid SASL.");
System.out.println();
System.exit(0);
}
String host = "localhost";
int port = 5672;
String source = "testqueue";
int nMsgs = 100;
String qosS = "EXACTLY_ONCE";
int txSize = 10;
boolean authAnon = true;
String user = null;
String password = null;
if (args.length >= 1)
host = args[0];
if (args.length >= 2)
port = Integer.parseInt(args[1]);
if (args.length >= 3)
source = args[2];
if (args.length >= 4)
nMsgs = Integer.parseInt(args[3]);
if (args.length >= 5)
qosS = args[4];
if (args.length >= 6)
txSize = Integer.parseInt(args[5]);
if (args.length >= 7)
authAnon = Boolean.parseBoolean(args[6]);
if (args.length >= 8)
user = args[7];
if (args.length >= 9)
password = args[8];
System.out.println();
System.out.println("Host : " + host);
System.out.println("Port : " + port);
System.out.println("Source : " + source);
System.out.println("Number Msgs : " + nMsgs);
System.out.println("QoS : " + qosS);
System.out.println("Tx Size : " + txSize);
System.out.println("Auth as Anon: " + authAnon);
System.out.println("User : " + user);
System.out.println("Password : " + password);
System.out.println();
try {
// Create connection and connect
AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
Connection connection = null;
if (args.length < 8)
connection = new Connection(ctx, host, port, authAnon);
else
connection = new Connection(ctx, host, port, user, password);
if (port == 5671) {
System.out.println("Using SSL on port 5671");
connection.setSocketFactory(new JSSESocketFactory());
}
connection.connect();
// Create session and consumer
Session session = connection.createSession(50, 50);
// Important to create the consumer without a link credit because the link credit is set by the acquisition
Consumer c = session.createConsumer(source, toIntQoS(qosS), true, null);
// Get the transaction controller
TransactionController txc = session.getTransactionController();
// Receive messages in transactions in size <txSize>
int currentTxSize = 0;
TxnIdIF txnId = txc.createTxnId();
c.acquire(txSize, txnId);
for (int i = 0; i < nMsgs; i++) {
AMQPMessage msg = c.receive();
if (msg == null)
break;
AmqpValue value = msg.getAmqpValue();
System.out.println("Received: " + ((AMQPString) value.getValue()).getValue());
msg.accept();
currentTxSize++;
if ((i + 1) % txSize == 0) {
txc.commit(txnId);
txnId = txc.createTxnId();
c.acquire(txSize, txnId);
currentTxSize = 0;
}
}
if (currentTxSize > 0)
txc.commit(txnId);
// Close everything down
Thread.sleep(2000);
c.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class SenderTransacted method main.
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("?")) {
System.out.println();
System.out.println("Usage: <host> <port> <target> <nmsgs> <qos> <txsize> <authanon> [<username> <password>]");
System.out.println(" <qos> ::= AT_LEAST_ONCE | AT_MOST_ONCE | EXACTLY_ONCE");
System.out.println(" Suppress <username> <password> and set <authanon> to false to avoid SASL.");
System.out.println();
System.exit(0);
}
String host = "localhost";
int port = 5672;
String target = "testqueue";
int nMsgs = 100;
String qosS = "EXACTLY_ONCE";
int txSize = 10;
boolean authAnon = true;
String user = null;
String password = null;
if (args.length >= 1)
host = args[0];
if (args.length >= 2)
port = Integer.parseInt(args[1]);
if (args.length >= 3)
target = args[2];
if (args.length >= 4)
nMsgs = Integer.parseInt(args[3]);
if (args.length >= 5)
qosS = args[4];
if (args.length >= 6)
txSize = Integer.parseInt(args[5]);
if (args.length >= 7)
authAnon = Boolean.parseBoolean(args[6]);
if (args.length >= 8)
user = args[7];
if (args.length >= 9)
password = args[8];
System.out.println();
System.out.println("Host : " + host);
System.out.println("Port : " + port);
System.out.println("Target : " + target);
System.out.println("Number Msgs : " + nMsgs);
System.out.println("QoS : " + qosS);
System.out.println("Tx Size : " + txSize);
System.out.println("Auth as Anon: " + authAnon);
System.out.println("User : " + user);
System.out.println("Password : " + password);
System.out.println();
try {
// Create connection and connect
AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
Connection connection = null;
if (args.length < 8)
connection = new Connection(ctx, host, port, authAnon);
else
connection = new Connection(ctx, host, port, user, password);
if (port == 5671) {
System.out.println("Using SSL on port 5671");
connection.setSocketFactory(new JSSESocketFactory());
}
connection.connect();
// Create session and producer
Session session = connection.createSession(50, 50);
Producer p = session.createProducer(target, toIntQoS(qosS));
// Get the transaction controller
TransactionController txc = session.getTransactionController();
// Send messages in transactions in size <txSize>
int currentTxSize = 0;
TxnIdIF txnId = txc.createTxnId();
for (int i = 0; i < nMsgs; i++) {
AMQPMessage msg = new AMQPMessage();
String s = "Message #" + (i + 1);
System.out.println("Sending " + s);
msg.setAmqpValue(new AmqpValue(new AMQPString(s)));
msg.setTxnIdIF(txnId);
p.send(msg);
currentTxSize++;
if ((i + 1) % txSize == 0) {
txc.commit(txnId);
txnId = txc.createTxnId();
currentTxSize = 0;
}
}
if (currentTxSize > 0)
txc.commit(txnId);
// Close everything down
Thread.sleep(2000);
p.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-client by iitsoftware.
the class AMQPMessage method accept.
/**
* Accepts delivery of this message. This is part of settlement with QoS modes at-least-once and exactly-once.
*
* @throws InvalidStateException If the state is invalid to perform this operation
*/
public void accept() throws InvalidStateException {
if (consumer == null)
throw new InvalidStateException("Message not associated with a consumer");
if (settled && txnIdIF == null)
throw new InvalidStateException("Accept not required; message has already been settled");
DeliveryStateIF deliveryStateIF = null;
if (txnIdIF == null)
deliveryStateIF = new Accepted();
else {
TransactionalState transactionalState = new TransactionalState();
transactionalState.setTxnId(txnIdIF);
transactionalState.setOutcome(new Accepted());
deliveryStateIF = transactionalState;
}
consumer.sendDisposition(this, deliveryStateIF);
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-client by iitsoftware.
the class AMQPMessage method reject.
/**
* <p>Rejects delivery of this message. This is part of settlement with QoS modes at-least-once and exactly-once.
* </p>
* <p>
* This operation causes the delivery count to be incremented and the message to be redelivered to this or other
* competing consumers.
* </p>
*
* @throws InvalidStateException If the state is invalid to perform this operation
*/
public void reject() throws InvalidStateException {
if (consumer == null)
throw new InvalidStateException("Message not associated with a consumer");
if (settled && txnIdIF == null)
throw new InvalidStateException("Reject not possible; message has already been settled");
DeliveryStateIF deliveryStateIF = null;
if (txnIdIF == null)
deliveryStateIF = new Rejected();
else {
TransactionalState transactionalState = new TransactionalState();
transactionalState.setTxnId(txnIdIF);
transactionalState.setOutcome(new Rejected());
deliveryStateIF = transactionalState;
}
consumer.sendDisposition(this, deliveryStateIF);
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class TargetLink method transformAndStore.
private void transformAndStore(TransferFrame frame) throws EndWithErrorException {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, frame=" + frame);
if (mySessionHandler.maxMessageSize > 0 && frame.getPayloadLength() > mySessionHandler.maxMessageSize)
throw new LinkEndException(this, LinkError.MESSAGE_SIZE_EXCEEDED, new AMQPString("Message size (" + frame.getPayloadLength() + ") > max message size (" + mySessionHandler.maxMessageSize));
if (coordinator)
handleTransactionRequest(frame);
else {
try {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, localDestination=" + localDestination);
long payloadLength = frame.getPayloadLength();
MessageImpl msg = getTransformer(frame.getMessageFormat().getValue()).transform(frame, this);
if (msg.getJMSDestination() == null)
msg.setJMSDestination(getLocalDestination());
msg.setStringProperty(MessageImpl.PROP_CLIENT_ID, mySessionHandler.getVersionedConnection().getActiveLogin().getClientId());
if (remoteUnsettled != null) {
DeliveryTag deliveryTag = frame.getDeliveryTag();
if (remoteUnsettled.remove(deliveryTag) != null) {
msg.setBooleanProperty(MessageImpl.PROP_DOUBT_DUPLICATE, true);
if (remoteUnsettled.size() == 0)
remoteUnsettled = null;
}
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, msg=" + msg);
DeliveryStateIF state = frame.getState();
if (state != null && state instanceof TransactionalState) {
TransactionalState txState = (TransactionalState) state;
TxnIdIF txnId = txState.getTxnId();
if (txnId != null) {
transactionRegistry.addToTransaction(txnId, name, msg, this);
} else
throw new SessionEndException(TransactionError.UNKNOWN_ID, new AMQPString("Missing TxnId in TransactionalState: " + txState));
} else {
QueuePushTransaction tx = sender.createTransaction();
tx.putMessage(msg);
tx.commit();
setFlowcontrolDelay(tx.getFlowControlDelay());
}
} catch (Exception e) {
throw new SessionEndException(AmqpError.INTERNAL_ERROR, new AMQPString("Exception during transformAndStore: " + e));
}
}
}
Aggregations