use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.
the class ActiveMQCrashTest method testHang.
@Test
public void testHang() throws Exception {
Configuration configuration = createDefaultInVMConfig().setPersistenceEnabled(false);
server = addServer(ActiveMQServers.newActiveMQServer(configuration));
server.start();
server.getRemotingService().addIncomingInterceptor(new AckInterceptor(server));
// Force an ack at once - this means the send call will block
locator.setConfirmationWindowSize(1);
ClientSessionFactory clientSessionFactory = createSessionFactory(locator);
ClientSession session = clientSessionFactory.createSession();
session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {
@Override
public void sendAcknowledged(final Message message) {
ackReceived = true;
}
});
ClientProducer producer = session.createProducer("fooQueue");
ClientMessage msg = session.createMessage(false);
msg.putStringProperty("someKey", "someValue");
producer.send(msg);
Thread.sleep(250);
Assert.assertFalse(ackReceived);
session.close();
}
use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.
the class ClientProducerImpl method largeMessageSendServer.
/**
* Used to send serverMessages through the bridges. No need to validate compression here since
* the message is only compressed at the client
*
* @param sendBlocking
* @param msgI
* @param handler
* @throws ActiveMQException
*/
private void largeMessageSendServer(final boolean sendBlocking, final ICoreMessage msgI, final ClientProducerCredits credits, SendAcknowledgementHandler handler) throws ActiveMQException {
sendInitialLargeMessageHeader(msgI, credits);
LargeBodyEncoder context = msgI.getBodyEncoder();
final long bodySize = context.getLargeBodySize();
context.open();
try {
for (long pos = 0; pos < bodySize; ) {
final boolean lastChunk;
final int chunkLength = (int) Math.min((bodySize - pos), minLargeMessageSize);
final ActiveMQBuffer bodyBuffer = ActiveMQBuffers.fixedBuffer(chunkLength);
context.encode(bodyBuffer, chunkLength);
pos += chunkLength;
lastChunk = pos >= bodySize;
SendAcknowledgementHandler messageHandler = lastChunk ? handler : null;
int creditsUsed = sessionContext.sendServerLargeMessageChunk(msgI, -1, sendBlocking, lastChunk, bodyBuffer.toByteBuffer().array(), messageHandler);
credits.acquireCredits(creditsUsed);
}
} finally {
context.close();
}
}
use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.
the class SendAckFailTest method testSend.
@Test
public void testSend() throws Exception {
Process process = SpawnedVMSupport.spawnVM(SendAckFailTest.class.getName());
ActiveMQServer server = null;
try {
HashSet<Integer> listSent = new HashSet<>();
Thread t = null;
{
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
ServerLocator locator = factory.getServerLocator();
locator.setConfirmationWindowSize(0).setInitialConnectAttempts(100).setRetryInterval(100).setBlockOnDurableSend(false).setReconnectAttempts(0);
ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
session.createAddress(SimpleString.toSimpleString("T1"), RoutingType.ANYCAST, true);
session.createQueue(SimpleString.toSimpleString("T1"), RoutingType.ANYCAST, SimpleString.toSimpleString("T1"), true);
ClientProducer producer = session.createProducer("T1");
session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {
@Override
public void sendAcknowledged(Message message) {
listSent.add(message.getIntProperty("myid"));
}
});
t = new Thread() {
@Override
public void run() {
for (int i = 0; i < 5000; i++) {
try {
producer.send(session.createMessage(true).putIntProperty("myid", i));
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
};
t.start();
}
Wait.waitFor(() -> listSent.size() > 100, 5000, 10);
Assert.assertTrue(process.waitFor(1, TimeUnit.MINUTES));
server = startServer(false);
{
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
ServerLocator locator = factory.getServerLocator();
ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consumer = session.createConsumer("T1");
session.start();
for (int i = 0; i < listSent.size(); i++) {
ClientMessage message = consumer.receive(1000);
if (message == null) {
for (Integer msgi : listSent) {
System.out.println("Message " + msgi + " was lost");
}
fail("missed messages!");
}
message.acknowledge();
if (!listSent.remove(message.getIntProperty("myid"))) {
System.out.println("Message " + message + " with id " + message.getIntProperty("myid") + " received in duplicate");
fail("Message " + message + " with id " + message.getIntProperty("myid") + " received in duplicate");
}
}
}
} finally {
if (process != null) {
process.destroy();
}
if (server != null) {
server.stop();
}
}
}
use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.
the class SendAcknowledgementsExample method main.
public static void main(final String[] args) throws Exception {
Connection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4. Create a JMS Connection
connection = cf.createConnection();
// Step 5. Define a SendAcknowledgementHandler which will receive asynchronous acknowledgements
class MySendAcknowledgementsHandler implements SendAcknowledgementHandler {
int count = 0;
@Override
public void sendAcknowledged(final Message message) {
System.out.println("Received send acknowledgement for message " + count++);
}
}
// Step 6. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 7. Set the handler on the underlying core session
ClientSession coreSession = ((ActiveMQSession) session).getCoreSession();
coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Step 7. Send 5000 messages, the handler will get called asynchronously some time later after the messages
// are sent.
final int numMessages = 5000;
for (int i = 0; i < numMessages; i++) {
javax.jms.Message jmsMessage = session.createMessage();
producer.send(jmsMessage);
System.out.println("Sent message " + i);
}
} finally {
// Step 12. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.
the class SessionSendAcknowledgementHandlerTest method testSetInvalidSendACK.
@Test
public void testSetInvalidSendACK() throws Exception {
ServerLocator locator = createInVMNonHALocator();
locator.setConfirmationWindowSize(-1);
ClientSessionFactory csf = createSessionFactory(locator);
ClientSession session = csf.createSession(null, null, false, true, true, false, 1);
boolean failed = false;
try {
session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {
@Override
public void sendAcknowledged(Message message) {
}
});
} catch (Throwable expected) {
failed = true;
}
assertTrue("Expected a failure on setting ACK Handler", failed);
session.createQueue(address, queueName, false);
}
Aggregations