use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class ReceiverTransactedRetirement 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);
Consumer c = session.createConsumer(source, 100, 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();
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.setTxnIdIF(txnId);
msg.accept();
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);
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 TransactionRegistry method addToTransaction.
public void addToTransaction(TxnIdIF btxnId, SourceLink sourceLink, long deliveryId, MessageIndex messageIndex, long size) throws EndWithErrorException {
String txnId = new String(((TransactionId) btxnId).getValue());
String linkName = sourceLink.getName();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/addToTransaction, txnId=" + txnId + ", linkName=" + linkName + ", messageIndex=" + messageIndex);
lock.lock();
try {
ActiveTxEntry activeTxEntry = (ActiveTxEntry) activeTx.get(txnId);
if (activeTxEntry == null) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/addToTransaction, txnId=" + txnId + ", not found!");
throw new SessionEndException(TransactionError.UNKNOWN_ID, new AMQPString("Transaction id not found: " + txnId));
}
sourceLink.increaseActiveTransactions();
if (activeTxEntry.outboundTxEntryMap == null)
activeTxEntry.outboundTxEntryMap = new HashMap();
OutboundTxEntry outboundTxEntry = (OutboundTxEntry) activeTxEntry.outboundTxEntryMap.get(deliveryId);
if (outboundTxEntry == null) {
outboundTxEntry = new OutboundTxEntry(sourceLink, messageIndex, null);
activeTxEntry.outboundTxEntryMap.put(deliveryId, outboundTxEntry);
} else {
outboundTxEntry.sourceLink = sourceLink;
outboundTxEntry.messageIndex = messageIndex;
}
} finally {
lock.unlock();
}
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class Sender method send.
public void send() {
try {
int txSize = 0;
TxnIdIF txnIdIF = txc.createTxnId();
boolean rollback = false;
int i = 0;
while (i < nMsgs) {
AMQPMessage msg = messageFactory.create(i);
msg.setTxnIdIF(txnIdIF);
producer.send(msg, persistent, 5, -1);
txSize++;
if (txSize == txSendSize) {
if (rollback)
txc.rollback(txnIdIF);
else {
txc.commit(txnIdIF);
i += txSize;
}
rollback = !rollback;
txnIdIF = txc.createTxnId();
txSize = 0;
}
}
if (txSize > 0) {
if (rollback)
txc.rollback(txnIdIF);
else
txc.commit(txnIdIF);
rollback = !rollback;
txSize = 0;
}
} catch (Exception e) {
fail("test failed: " + e);
}
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class Receiver method receive.
public void receive() {
try {
int txSize = 0;
TxnIdIF txnIdIF = txc.createTxnId();
consumer.acquire(txRcvSize, txnIdIF);
for (int i = 0; i < nMsgs; i++) {
AMQPMessage msg = consumer.receive();
if (msg != null) {
messageFactory.verify(msg);
if (!msg.isSettled())
msg.accept();
txSize++;
if (txSize == txRcvSize) {
txc.commit(txnIdIF);
txnIdIF = txc.createTxnId();
consumer.acquire(txRcvSize, txnIdIF);
txSize = 0;
}
} else
throw new Exception("Msg == null at i=" + i);
}
if (txSize > 0) {
txc.commit(txnIdIF);
txSize = 0;
}
} catch (Exception e) {
fail("test failed: " + e);
}
}
use of com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF in project swiftmq-ce by iitsoftware.
the class Sender method send.
public void send() {
try {
int txSize = 0;
TxnIdIF txnIdIF = txc.createTxnId();
for (int i = 0; i < nMsgs; i++) {
AMQPMessage msg = messageFactory.create(i);
msg.setTxnIdIF(txnIdIF);
producer.send(msg, persistent, 5, -1);
txSize++;
if (txSize == txSendSize) {
txc.commit(txnIdIF);
txnIdIF = txc.createTxnId();
txSize = 0;
}
}
if (txSize > 0) {
txc.commit(txnIdIF);
txSize = 0;
}
} catch (Exception e) {
fail("test failed: " + e);
}
}
Aggregations