use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class JMSFailoverListenerTest method testAutomaticFailover.
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testAutomaticFailover() throws Exception {
ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
jbcf.setReconnectAttempts(-1);
jbcf.setBlockOnDurableSend(true);
jbcf.setBlockOnNonDurableSend(true);
// Note we set consumer window size to a value so we can verify that consumer credit re-sending
// works properly on failover
// The value is small enough that credits will have to be resent several time
final int numMessages = 10;
final int bodySize = 1000;
jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
ActiveMQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
MyFailoverListener listener = new MyFailoverListener();
conn.setFailoverListener(listener);
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
SimpleString jmsQueueName = new SimpleString("myqueue");
coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
Queue queue = sess.createQueue("myqueue");
MessageProducer producer = sess.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
MessageConsumer consumer = sess.createConsumer(queue);
byte[] body = RandomUtil.randomBytes(bodySize);
for (int i = 0; i < numMessages; i++) {
BytesMessage bm = sess.createBytesMessage();
bm.writeBytes(body);
producer.send(bm);
}
conn.start();
JMSFailoverListenerTest.log.info("sent messages and started connection");
Thread.sleep(2000);
JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
for (int i = 0; i < numMessages; i++) {
JMSFailoverListenerTest.log.info("got message " + i);
BytesMessage bm = (BytesMessage) consumer.receive(1000);
Assert.assertNotNull(bm);
Assert.assertEquals(body.length, bm.getBodyLength());
}
TextMessage tm = (TextMessage) consumer.receiveNoWait();
Assert.assertNull(tm);
Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.get(1));
conn.close();
Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class JMSFailoverListenerTest method testManualFailover.
@Test
public void testManualFailover() throws Exception {
ActiveMQConnectionFactory jbcfLive = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
jbcfLive.setBlockOnNonDurableSend(true);
jbcfLive.setBlockOnDurableSend(true);
ActiveMQConnectionFactory jbcfBackup = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, backupParams));
jbcfBackup.setBlockOnNonDurableSend(true);
jbcfBackup.setBlockOnDurableSend(true);
jbcfBackup.setInitialConnectAttempts(-1);
jbcfBackup.setReconnectAttempts(-1);
ActiveMQConnection connLive = (ActiveMQConnection) jbcfLive.createConnection();
MyFailoverListener listener = new MyFailoverListener();
connLive.setFailoverListener(listener);
Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
SimpleString jmsQueueName = new SimpleString("myqueue");
coreSessionLive.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
Queue queue = sessLive.createQueue("myqueue");
final int numMessages = 1000;
MessageProducer producerLive = sessLive.createProducer(queue);
for (int i = 0; i < numMessages; i++) {
TextMessage tm = sessLive.createTextMessage("message" + i);
producerLive.send(tm);
}
// Note we block on P send to make sure all messages get to server before failover
JMSUtil.crash(liveServer, coreSessionLive);
Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
connLive.close();
// Now recreate on backup
Connection connBackup = jbcfBackup.createConnection();
Session sessBackup = connBackup.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumerBackup = sessBackup.createConsumer(queue);
connBackup.start();
for (int i = 0; i < numMessages; i++) {
TextMessage tm = (TextMessage) consumerBackup.receive(1000);
Assert.assertNotNull(tm);
Assert.assertEquals("message" + i, tm.getText());
}
TextMessage tm = (TextMessage) consumerBackup.receiveNoWait();
Assert.assertEquals(FailoverEventType.FAILOVER_FAILED, listener.get(1));
Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
Assert.assertNull(tm);
connBackup.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class ServerUtil method getServer.
public static int getServer(Connection connection) {
ClientSession session = ((ActiveMQConnection) connection).getInitialSession();
TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
String port = (String) transportConfiguration.getParams().get("port");
return Integer.valueOf(port) - 61616;
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class ServerUtil method getServerConnection.
public static Connection getServerConnection(int server, Connection... connections) {
for (Connection connection : connections) {
ClientSession session = ((ActiveMQConnection) connection).getInitialSession();
TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
String port = (String) transportConfiguration.getParams().get("port");
if (Integer.valueOf(port) == server + 61616) {
return connection;
}
}
return null;
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class JMSBridgeImpl method createConnection.
private Connection createConnection(final String username, final String password, final ConnectionFactoryFactory cff, final String clientID, final boolean isXA, boolean isSource) throws Exception {
Connection conn = null;
try {
Object cf = cff.createConnectionFactory();
if (cf instanceof ActiveMQConnectionFactory && registry != null) {
registry.register(XARecoveryConfig.newConfig((ActiveMQConnectionFactory) cf, username, password, null));
}
if (qualityOfServiceMode == QualityOfServiceMode.ONCE_AND_ONLY_ONCE && !(cf instanceof XAConnectionFactory)) {
throw new IllegalArgumentException("Connection factory must be XAConnectionFactory");
}
if (username == null) {
if (isXA) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
}
conn = ((XAConnectionFactory) cf).createXAConnection();
} else {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
}
conn = ((ConnectionFactory) cf).createConnection();
}
} else {
if (isXA) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
}
conn = ((XAConnectionFactory) cf).createXAConnection(username, password);
} else {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
}
conn = ((ConnectionFactory) cf).createConnection(username, password);
}
}
if (clientID != null) {
conn.setClientID(clientID);
}
boolean ha = false;
BridgeFailoverListener failoverListener = null;
if (conn instanceof ActiveMQConnection) {
ActiveMQConnectionFactory activeMQCF = (ActiveMQConnectionFactory) cf;
ha = activeMQCF.isHA();
if (ha) {
ActiveMQConnection activeMQConn = (ActiveMQConnection) conn;
failoverListener = new BridgeFailoverListener(isSource);
activeMQConn.setFailoverListener(failoverListener);
}
}
conn.setExceptionListener(new BridgeExceptionListener(ha, failoverListener, isSource));
return conn;
} catch (JMSException e) {
try {
if (conn != null) {
conn.close();
}
} catch (Throwable ignored) {
}
throw e;
}
}
Aggregations