use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class JMSFailoverTest method testAutomaticFailover.
@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);
Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
MyExceptionListener listener = new MyExceptionListener();
conn.setExceptionListener(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();
JMSFailoverTest.log.info("sent messages and started connection");
Thread.sleep(2000);
JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
for (int i = 0; i < numMessages; i++) {
JMSFailoverTest.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);
conn.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class JMSFailoverTest 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);
Connection connLive = jbcfLive.createConnection();
MyExceptionListener listener = new MyExceptionListener();
connLive.setExceptionListener(listener);
Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
RemotingConnection coreConnLive = ((ClientSessionInternal) coreSessionLive).getConnection();
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);
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.assertNull(tm);
connBackup.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class LargeMessageOverBridgeTest method testSendLargeForBridge.
/**
* The message won't be large to the client while it will be considered large through the bridge
*
* @throws Exception
*/
@Test
public void testSendLargeForBridge() throws Exception {
createQueue(QUEUE);
Queue queue = (Queue) context1.lookup("queue/" + QUEUE);
ActiveMQConnectionFactory cf1 = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, generateInVMParams(1)));
cf1.setMinLargeMessageSize(200 * 1024);
Connection conn1 = cf1.createConnection();
Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer prod1 = session1.createProducer(queue);
Connection conn2 = cf2.createConnection();
Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons2 = session2.createConsumer(queue);
conn2.start();
byte[] bytes = new byte[150 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = getSamplebyte(i);
}
for (int i = 0; i < 10; i++) {
BytesMessage msg = session1.createBytesMessage();
msg.writeBytes(bytes);
prod1.send(msg);
}
session1.commit();
for (int i = 0; i < 5; i++) {
BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
assertNotNull(msg2);
msg2.acknowledge();
for (int j = 0; j < bytes.length; j++) {
assertEquals("Position " + i, msg2.readByte(), bytes[j]);
}
}
conn1.close();
conn2.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ConnectionFactorySerializationTest method testConnectionFactoryStatic1.
@Test
public void testConnectionFactoryStatic1() throws Exception {
createStaticFactory(true);
cf = (ActiveMQConnectionFactory) namingContext.lookup("/MyConnectionFactory");
// apparently looking up the connection factory with the org.apache.activemq.artemis.jms.tests.tools.container.InVMInitialContextFactory
// is not enough to actually serialize it so we serialize it manually
byte[] x = serialize(cf);
ActiveMQConnectionFactory y = deserialize(x, ActiveMQConnectionFactory.class);
checkEquals(cf, y);
Assert.assertEquals(cf.isHA(), y.isHA());
TransportConfiguration[] staticConnectors = y.getStaticConnectors();
Assert.assertEquals(staticConnectors.length, 2);
TransportConfiguration tc0 = cf.getStaticConnectors()[0];
TransportConfiguration y0 = y.getStaticConnectors()[0];
Map<String, Object> ctParams = tc0.getParams();
Map<String, Object> y0Params = y0.getParams();
Assert.assertEquals(ctParams.size(), y0Params.size());
for (String key : y0Params.keySet()) {
Assert.assertEquals(ctParams.get(key), y0Params.get(key));
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ConnectionFactoryWithJGroupsSerializationTest method testSerialization.
// Public --------------------------------------------------------
// HORNETQ-1389
// Here we deploy two Connection Factories with JGroups discovery groups.
// The first one uses a runtime JChannel object, which is the case before the fix.
// The second one uses the raw jgroups config string, which is the case after fix.
// So the first one will get serialization exception in the test
// while the second will not.
@Test
public void testSerialization() throws Exception {
jmsCf1 = (ActiveMQConnectionFactory) namingContext.lookup("/ConnectionFactory1");
jmsCf2 = (ActiveMQConnectionFactory) namingContext.lookup("/ConnectionFactory2");
try {
serialize(jmsCf1);
} catch (java.io.NotSerializableException e) {
// this is expected
}
// now cf2 should be OK
byte[] x = serialize(jmsCf2);
ActiveMQConnectionFactory jmsCf2Copy = deserialize(x, ActiveMQConnectionFactory.class);
assertNotNull(jmsCf2Copy);
BroadcastEndpointFactory broadcastEndpoint = jmsCf2Copy.getDiscoveryGroupConfiguration().getBroadcastEndpointFactory();
assertTrue(broadcastEndpoint instanceof JGroupsFileBroadcastEndpointFactory);
}
Aggregations