Examples
Complete Examples
Basic Send and Receive with AT_LEAST_ONCE QoS
Demonstrates connecting, creating a session, sending a message to a queue, and receiving it with explicit acceptance.
import com.swiftmq.amqp.v100.client.*;
import com.swiftmq.amqp.v100.messaging.*;
public class SwiftMQBasicExample {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
Producer producer = null;
Consumer consumer = null;
try {
AMQPContext ctx = new AMQPContext();
connection = new Connection(ctx, "localhost", 5672, "user", "password");
connection.setContainerId("example-client");
connection.connect();
session = connection.createSession(100, 100);
producer = session.createProducer("myqueue@router1", QoS.AT_LEAST_ONCE);
consumer = session.createConsumer("myqueue@router1", 10, QoS.AT_LEAST_ONCE, false, null);
// Send a message
AMQPMessage msg = new AMQPMessage();
msg.setAmqpValue(new AmqpValue("Hello, AMQP!"));
producer.send(msg);
// Receive and accept the message
AMQPMessage received = consumer.receive(5000);
if (received != null && !received.isSettled()) {
System.out.println("Received: " + received.getAmqpValue().getValue());
received.accept();
} else {
System.out.println("No message received.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (consumer != null) consumer.close(); } catch (Exception ignored) {}
try { if (producer != null) producer.close(); } catch (Exception ignored) {}
if (session != null) session.close();
if (connection != null) connection.close();
}
}
}
Transactional Send and Commit
Shows how to send a message within a transaction and commit it.
import com.swiftmq.amqp.v100.client.*;
import com.swiftmq.amqp.v100.messaging.*;
public class SwiftMQTransactionExample {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
Producer producer = null;
try {
AMQPContext ctx = new AMQPContext();
connection = new Connection(ctx, "localhost", 5672, "user", "password");
connection.setContainerId("txn-client");
connection.connect();
session = connection.createSession(100, 100);
producer = session.createProducer("myqueue@router1", QoS.AT_LEAST_ONCE);
TransactionController txCtrl = session.getTransactionController();
TxnIdIF txnId = txCtrl.createTxnId();
AMQPMessage msg = new AMQPMessage();
msg.setAmqpValue(new AmqpValue("Transactional message"));
msg.setTxnIdIF(txnId);
producer.send(msg);
txCtrl.commit(txnId);
System.out.println("Message sent and committed in transaction.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (producer != null) producer.close(); } catch (Exception ignored) {}
if (session != null) session.close();
if (connection != null) connection.close();
}
}
}
Durable Consumer on a Topic
Creates a durable consumer on a topic, receives a message, and unsubscribes.
import com.swiftmq.amqp.v100.client.*;
import com.swiftmq.amqp.v100.messaging.*;
public class SwiftMQDurableConsumerExample {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
DurableConsumer durableConsumer = null;
try {
AMQPContext ctx = new AMQPContext();
connection = new Connection(ctx, "localhost", 5672, "user", "password");
connection.setContainerId("durable-client");
connection.connect();
session = connection.createSession(100, 100);
durableConsumer = session.createDurableConsumer(
"myDurableLink", // linkName
"mytopic", // source (topic name, no @router)
10, // linkCredit
QoS.AT_LEAST_ONCE,
false,
null
);
AMQPMessage msg = durableConsumer.receive(5000);
if (msg != null && !msg.isSettled()) {
System.out.println("Received: " + msg.getAmqpValue().getValue());
msg.accept();
} else {
System.out.println("No message received.");
}
// Unsubscribe and destroy the durable link
durableConsumer.unsubscribe();
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (durableConsumer != null) durableConsumer.close(); } catch (Exception ignored) {}
if (session != null) session.close();
if (connection != null) connection.close();
}
}
}