use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ClusteredQueueExample method main.
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
Connection connection1 = null;
try {
// Step 2. Instantiate the Queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Instantiate connection towards server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
// Step 7. We create a JMS Connection connection1 which is a connection to server 1
connection1 = cf1.createConnection();
// Step 8. We create a JMS Session on server 0
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 9. We create a JMS Session on server 1
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 10. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 11. We create JMS MessageConsumer objects on server 0 and server 1
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
Thread.sleep(1000);
// Step 12. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(queue);
// Step 13. We send some messages to server 0
final int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
for (int i = 0; i < numMessages; i += 2) {
TextMessage message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage) consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
} finally {
if (connection0 != null) {
connection0.close();
}
if (connection1 != null) {
connection1.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class PagingTest method testPurge.
@Test
public void testPurge() throws Exception {
clearDataRecreateServerDirs();
Configuration config = createDefaultNettyConfig().setJournalSyncNonTransactional(false);
server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
server.start();
String queue = "purgeQueue";
SimpleString ssQueue = new SimpleString(queue);
server.addAddressInfo(new AddressInfo(ssQueue, RoutingType.ANYCAST));
QueueImpl purgeQueue = (QueueImpl) server.createQueue(ssQueue, RoutingType.ANYCAST, ssQueue, null, true, false, 1, true, false);
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
Connection connection = cf.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
javax.jms.Queue jmsQueue = session.createQueue(queue);
MessageProducer producer = session.createProducer(jmsQueue);
for (int i = 0; i < 100; i++) {
producer.send(session.createTextMessage("hello" + i));
}
session.commit();
Wait.assertEquals(0, purgeQueue::getMessageCount);
Assert.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore().getAddressSize());
MessageConsumer consumer = session.createConsumer(jmsQueue);
for (int i = 0; i < 100; i++) {
producer.send(session.createTextMessage("hello" + i));
if (i == 10) {
purgeQueue.getPageSubscription().getPagingStore().startPaging();
}
}
session.commit();
consumer.close();
Wait.assertEquals(0, purgeQueue::getMessageCount);
Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
purgeQueue.getPageSubscription().getPagingStore().startPaging();
Wait.assertTrue(purgeQueue.getPageSubscription()::isPaging);
consumer = session.createConsumer(jmsQueue);
for (int i = 0; i < 100; i++) {
purgeQueue.getPageSubscription().getPagingStore().startPaging();
Assert.assertTrue(purgeQueue.getPageSubscription().isPaging());
producer.send(session.createTextMessage("hello" + i));
if (i % 2 == 0) {
session.commit();
}
}
session.commit();
connection.start();
server.getStorageManager().getMessageJournal().scheduleCompactAndBlock(50000);
Assert.assertNotNull(consumer.receive(5000));
session.commit();
consumer.close();
Wait.assertEquals(0, purgeQueue::getMessageCount);
Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
StorageManager sm = server.getStorageManager();
for (int i = 0; i < 1000; i++) {
long tx = sm.generateID();
PageTransactionInfoImpl txinfo = new PageTransactionInfoImpl(tx);
sm.storePageTransaction(tx, txinfo);
sm.commit(tx);
tx = sm.generateID();
sm.updatePageTransaction(tx, txinfo, 1);
sm.commit(tx);
}
server.stop();
server.start();
Assert.assertEquals(0, server.getPagingManager().getTransactions().size());
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ActiveMQClusteredTest method testOutboundLoadBalancing.
@Test
public void testOutboundLoadBalancing() throws Exception {
final int CONNECTION_COUNT = 100;
ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
List<Session> sessions = new ArrayList<>();
List<ActiveMQRAManagedConnection> managedConnections = new ArrayList<>();
try {
MyBootstrapContext ctx = new MyBootstrapContext();
qResourceAdapter.start(ctx);
ActiveMQRAConnectionManager qraConnectionManager = new ActiveMQRAConnectionManager();
ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
mcf.setResourceAdapter(qResourceAdapter);
ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
Session s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
sessions.add(s);
ActiveMQRAManagedConnection mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
managedConnections.add(mc);
ActiveMQConnectionFactory cf1 = mc.getConnectionFactory();
while (!((ServerLocatorImpl) cf1.getServerLocator()).isReceivedTopology()) {
Thread.sleep(50);
}
for (int i = 0; i < CONNECTION_COUNT; i++) {
queueConnection = qraConnectionFactory.createQueueConnection();
s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
sessions.add(s);
mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
managedConnections.add(mc);
}
assertTrue(server.getConnectionCount() >= (CONNECTION_COUNT / 2));
assertTrue(secondaryServer.getConnectionCount() >= (CONNECTION_COUNT / 2));
} finally {
for (Session s : sessions) {
s.close();
}
for (ActiveMQRAManagedConnection mc : managedConnections) {
mc.destroy();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class OutgoingConnectionTest method testSharedActiveMQConnectionFactory.
@Test
public void testSharedActiveMQConnectionFactory() throws Exception {
Session s = null;
Session s2 = null;
ActiveMQRAManagedConnection mc = null;
ActiveMQRAManagedConnection mc2 = null;
try {
resourceAdapter = new ActiveMQResourceAdapter();
resourceAdapter.setConnectorClassName(InVMConnectorFactory.class.getName());
MyBootstrapContext ctx = new MyBootstrapContext();
resourceAdapter.start(ctx);
ActiveMQRAConnectionManager qraConnectionManager = new ActiveMQRAConnectionManager();
ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
mcf.setResourceAdapter(resourceAdapter);
ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
ActiveMQConnectionFactory cf1 = mc.getConnectionFactory();
QueueConnection queueConnection2 = qraConnectionFactory.createQueueConnection();
s2 = queueConnection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
mc2 = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s2).getManagedConnection();
ActiveMQConnectionFactory cf2 = mc2.getConnectionFactory();
// we're not testing equality so don't use equals(); we're testing if they are actually the *same* object
assertTrue(cf1 == cf2);
} finally {
if (s != null) {
s.close();
}
if (mc != null) {
mc.destroy();
}
if (s2 != null) {
s2.close();
}
if (mc2 != null) {
mc2.destroy();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ResourceAdapterTest method testResourceAdapterSetupReconnectAttemptsOverride.
@Test
public void testResourceAdapterSetupReconnectAttemptsOverride() throws Exception {
ActiveMQResourceAdapter qResourceAdapter = new ActiveMQResourceAdapter();
qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY);
qResourceAdapter.setConnectionParameters("server-id=0");
ActiveMQRATestBase.MyBootstrapContext ctx = new ActiveMQRATestBase.MyBootstrapContext();
qResourceAdapter.start(ctx);
ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
spec.setResourceAdapter(qResourceAdapter);
spec.setUseJNDI(false);
spec.setDestinationType("javax.jms.Queue");
spec.setDestination(MDBQUEUE);
spec.setReconnectAttempts(100);
ActiveMQConnectionFactory fac = qResourceAdapter.getConnectionFactory(spec);
assertEquals(100, fac.getReconnectAttempts());
qResourceAdapter.stop();
assertTrue(spec.isHasBeenUpdated());
}
Aggregations