use of javax.jms.QueueConnection in project activemq-artemis by apache.
the class OutgoingConnectionNoJTATest method testSimpleMessageSendAndReceiveSessionTransacted.
@Test
public void testSimpleMessageSendAndReceiveSessionTransacted() throws Exception {
setupDLQ(10);
resourceAdapter = newResourceAdapter();
MyBootstrapContext ctx = new MyBootstrapContext();
resourceAdapter.start(ctx);
ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
mcf.setAllowLocalTransactions(true);
mcf.setResourceAdapter(resourceAdapter);
ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
Session s = queueConnection.createSession(true, Session.SESSION_TRANSACTED);
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
MessageProducer mp = s.createProducer(q);
MessageConsumer consumer = s.createConsumer(q);
Message message = s.createTextMessage("test");
mp.send(message);
s.commit();
queueConnection.start();
TextMessage textMessage = (TextMessage) consumer.receive(1000);
assertNotNull(textMessage);
assertEquals(textMessage.getText(), "test");
s.rollback();
textMessage = (TextMessage) consumer.receive(1000);
assertNotNull(textMessage);
assertEquals(textMessage.getText(), "test");
s.commit();
textMessage = (TextMessage) consumer.receiveNoWait();
assertNull(textMessage);
}
use of javax.jms.QueueConnection in project activemq-artemis by apache.
the class CreateTemporaryQueueBeforeStartTest method testTemporaryQueueConsumer.
public void testTemporaryQueueConsumer() throws Exception {
final int number = 20;
final AtomicInteger count = new AtomicInteger(0);
for (int i = 0; i < number; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
QueueConnection connection = createConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createTemporaryQueue();
session.createReceiver(queue);
connection.start();
if (count.incrementAndGet() >= number) {
synchronized (count) {
count.notify();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
thread.start();
}
int maxWaitTime = 20000;
synchronized (count) {
long waitTime = maxWaitTime;
long start = System.currentTimeMillis();
while (count.get() < number) {
if (waitTime <= 0) {
break;
} else {
count.wait(waitTime);
waitTime = maxWaitTime - (System.currentTimeMillis() - start);
}
}
}
assertTrue("Unexpected count: " + count, count.get() == number);
}
use of javax.jms.QueueConnection in project activemq-artemis by apache.
the class ConnectionFactoryTest method testQueueConnectionFactory.
/**
* Test that ConnectionFactory can be cast to QueueConnectionFactory and QueueConnection can be
* created.
*/
@Test
public void testQueueConnectionFactory() throws Exception {
deployConnectionFactory(0, JMSFactoryType.QUEUE_CF, "CF_QUEUE_XA_FALSE", "/CF_QUEUE_XA_FALSE");
QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup("/CF_QUEUE_XA_FALSE");
QueueConnection qc = qcf.createQueueConnection();
qc.close();
undeployConnectionFactory("CF_QUEUE_XA_FALSE");
}
use of javax.jms.QueueConnection in project activemq-artemis by apache.
the class ClientKickoffExample method main.
public static void main(final String[] args) throws Exception {
QueueConnection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perform a lookup on the Connection Factory
QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 3.Create a JMS Connection
connection = cf.createQueueConnection();
// Step 4. Set an exception listener on the connection to be notified after a problem occurred
final AtomicReference<JMSException> exception = new AtomicReference<>();
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(final JMSException e) {
exception.set(e);
}
});
// Step 5. We start the connection
connection.start();
// Step 6. Create an ActiveMQServerControlMBean proxy to manage the server
ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ActiveMQServerControl serverControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, ActiveMQServerControl.class, false);
// Step 7. List the remote address connected to the server
System.out.println("List of remote addresses connected to the server:");
System.out.println("----------------------------------");
String[] remoteAddresses = serverControl.listRemoteAddresses();
for (String remoteAddress : remoteAddresses) {
System.out.println(remoteAddress);
}
System.out.println("----------------------------------");
// Step 8. Close the connections for the 1st remote address and kickoff the client
serverControl.closeConnectionsForAddress(remoteAddresses[0]);
// Sleep a little bit so that the stack trace from the server won't be
// mingled with the JMSException received on the ExceptionListener
Thread.sleep(1000);
// Step 9. Display the exception received by the connection's ExceptionListener
System.err.println("\nException received from the server:");
System.err.println("----------------------------------");
exception.get().printStackTrace();
System.err.println("----------------------------------");
} finally {
// Step 10. Be sure to close the resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of javax.jms.QueueConnection in project activemq-artemis by apache.
the class PreacknowledgeExample method getMessageCount.
// To do this we send a management message to get the message count.
// In real life you wouldn't create a new session every time you send a management message
private static int getMessageCount(final Connection connection) throws Exception {
QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
connection.start();
Message m = session.createMessage();
JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
Message response = requestor.request(m);
int messageCount = (Integer) JMSManagementHelper.getResult(response, Integer.class);
return messageCount;
}
Aggregations