use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class ReattachTest method testImmediateReattach.
/*
* Test failure on connection, but server is still up so should immediately reconnect
*/
@Test
public void testImmediateReattach() throws Exception {
final long retryInterval = 50;
final double retryMultiplier = 1d;
final int reconnectAttempts = 10;
locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
ClientSession session = sf.createSession(false, true, true);
session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
final int numIterations = 10;
for (int j = 0; j < numIterations; j++) {
ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
final int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
message.putIntProperty(new SimpleString("count"), i);
message.getBodyBuffer().writeString("aardvarks");
producer.send(message);
}
ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
conn.fail(new ActiveMQNotConnectedException());
session.start();
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer.receive(500);
Assert.assertNotNull(message);
Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
Assert.assertEquals(i, message.getObjectProperty(new SimpleString("count")));
message.acknowledge();
}
ClientMessage message = consumer.receiveImmediate();
Assert.assertNull(message);
producer.close();
consumer.close();
}
session.close();
sf.close();
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class AsynchronousFailoverTest method runTest.
private void runTest(final TestRunner runnable) throws Throwable {
final int numIts = 1;
try {
for (int i = 0; i < numIts; i++) {
AsynchronousFailoverTest.log.info("Iteration " + i);
ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(15).setConfirmationWindowSize(10 * 1024 * 1024);
sf = createSessionFactoryAndWaitForTopology(locator, 2);
try {
ClientSession createSession = sf.createSession(true, true);
createSession.createQueue(FailoverTestBase.ADDRESS, FailoverTestBase.ADDRESS, (SimpleString) null, true);
RemotingConnection conn = ((ClientSessionInternal) createSession).getConnection();
Thread t = new Thread(runnable);
t.setName("MainTEST");
t.start();
long randomDelay = (long) (2000 * Math.random());
AsynchronousFailoverTest.log.info("Sleeping " + randomDelay);
Thread.sleep(randomDelay);
AsynchronousFailoverTest.log.info("Failing asynchronously");
// Simulate failure on connection
synchronized (lockFail) {
if (log.isDebugEnabled()) {
log.debug("#test crashing test");
}
crash(createSession);
}
/*if (listener != null)
{
boolean ok = listener.latch.await(10000, TimeUnit.MILLISECONDS);
Assert.assertTrue(ok);
}*/
runnable.setFailed();
AsynchronousFailoverTest.log.info("Fail complete");
t.join(TimeUnit.SECONDS.toMillis(60));
if (t.isAlive()) {
System.out.println(threadDump("Thread still running from the test"));
t.interrupt();
fail("Test didn't complete successful, thread still running");
}
runnable.checkForExceptions();
createSession.close();
Assert.assertEquals(0, sf.numSessions());
locator.close();
} finally {
locator.close();
Assert.assertEquals(0, sf.numConnections());
}
if (i != numIts - 1) {
tearDown();
runnable.checkForExceptions();
runnable.reset();
setUp();
}
}
} finally {
}
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class NettyAsynchronousReattachTest method crash.
@Override
protected void crash(final ClientSession... sessions) throws Exception {
for (ClientSession session : sessions) {
log.debug("Crashing session " + session);
ClientSessionInternal internalSession = (ClientSessionInternal) session;
internalSession.getConnection().fail(new ActiveMQNotConnectedException("oops"));
}
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class ActiveMQRAManagedConnection method getXAResource.
/**
* Get the XAResource for the connection.
*
* @return The XAResource for the connection.
* @throws ResourceException XA transaction not supported
*/
@Override
public XAResource getXAResource() throws ResourceException {
if (logger.isTraceEnabled()) {
ActiveMQRALogger.LOGGER.trace("getXAResource()");
}
//
if (xaResource == null) {
ClientSessionInternal csi = (ClientSessionInternal) xaSession.getXAResource();
ActiveMQRAXAResource activeMQRAXAResource = new ActiveMQRAXAResource(this, xaSession.getXAResource());
Map<String, Object> xaResourceProperties = new HashMap<>();
xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_JNDI_NAME, ra.getJndiName());
xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_NODE_ID, csi.getNodeId());
xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_NAME, ActiveMQResourceAdapter.PRODUCT_NAME);
xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_VERSION, VersionLoader.getVersion().getFullVersion());
xaResource = ServiceUtils.wrapXAResource(activeMQRAXAResource, xaResourceProperties);
}
if (logger.isTraceEnabled()) {
ActiveMQRALogger.LOGGER.trace("XAResource=" + xaResource);
}
return xaResource;
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class ReconnectTest method internalTestInterruptReconnect.
public void internalTestInterruptReconnect(final boolean isNetty, final boolean interruptMainThread) throws Exception {
final int pingPeriod = 1000;
ActiveMQServer server = createServer(false, isNetty);
server.start();
try {
ServerLocator locator = createFactory(isNetty).setClientFailureCheckPeriod(pingPeriod).setRetryInterval(500).setRetryIntervalMultiplier(1d).setReconnectAttempts(-1).setConfirmationWindowSize(1024 * 1024);
ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) locator.createSessionFactory();
// One for beforeReconnecto from the Factory, and one for the commit about to be done
final CountDownLatch latchCommit = new CountDownLatch(2);
final ArrayList<Thread> threadToBeInterrupted = new ArrayList<>();
factory.addFailureListener(new SessionFailureListener() {
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver) {
}
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
connectionFailed(me, failedOver);
}
@Override
public void beforeReconnect(ActiveMQException exception) {
threadToBeInterrupted.add(Thread.currentThread());
System.out.println("Thread " + Thread.currentThread() + " reconnecting now");
latchCommit.countDown();
}
});
final ClientSessionInternal session = (ClientSessionInternal) factory.createSession();
final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1);
session.addFailureListener(new SessionFailureListener() {
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver) {
count.incrementAndGet();
latch.countDown();
}
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
connectionFailed(me, failedOver);
}
@Override
public void beforeReconnect(final ActiveMQException exception) {
}
});
server.stop();
Thread tcommitt = new Thread() {
@Override
public void run() {
latchCommit.countDown();
try {
session.commit();
} catch (ActiveMQException e) {
e.printStackTrace();
}
}
};
tcommitt.start();
assertTrue(latchCommit.await(10, TimeUnit.SECONDS));
// There should be only one thread
assertEquals(1, threadToBeInterrupted.size());
if (interruptMainThread) {
tcommitt.interrupt();
} else {
for (Thread tint : threadToBeInterrupted) {
tint.interrupt();
}
}
tcommitt.join(5000);
assertFalse(tcommitt.isAlive());
locator.close();
} finally {
}
}
Aggregations