use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class ActiveMQRASession method getNodeId.
/**
* Returns the ID of the Node that this session is associated with.
*
* @return Node ID
*/
public String getNodeId() throws JMSException {
ActiveMQSession session = (ActiveMQSession) getSessionInternal();
ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) session.getCoreSession().getSessionFactory();
return factory.getLiveNodeId();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class FailureDeadlockTest method testDeadlock.
// https://jira.jboss.org/jira/browse/JBMESSAGING-1702
// Test that two failures concurrently executing and calling the same exception listener
// don't deadlock
@Test
public void testDeadlock() throws Exception {
for (int i = 0; i < 100; i++) {
final Connection conn1 = cf1.createConnection();
Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
RemotingConnection rc1 = ((ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession()).getConnection();
final Connection conn2 = cf2.createConnection();
Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
RemotingConnection rc2 = ((ClientSessionInternal) ((ActiveMQSession) sess2).getCoreSession()).getConnection();
ExceptionListener listener1 = new ExceptionListener() {
@Override
public void onException(final JMSException exception) {
try {
conn2.close();
} catch (Exception e) {
FailureDeadlockTest.log.error("Failed to close connection2", e);
}
}
};
conn1.setExceptionListener(listener1);
conn2.setExceptionListener(listener1);
Failer f1 = new Failer(rc1);
Failer f2 = new Failer(rc2);
f1.start();
f2.start();
f1.join();
f2.join();
conn1.close();
conn2.close();
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class FailureDeadlockTest method testUsingDeadConnection.
// https://jira.jboss.org/jira/browse/JBMESSAGING-1703
// Make sure that failing a connection removes it from the connection manager and can't be returned in a subsequent
// call
@Test
public void testUsingDeadConnection() throws Exception {
for (int i = 0; i < 100; i++) {
final Connection conn1 = cf1.createConnection();
Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
RemotingConnection rc1 = ((ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession()).getConnection();
rc1.fail(new ActiveMQNotConnectedException("blah"));
try {
conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
Assert.fail("should throw exception");
} catch (JMSException e) {
// pass
}
conn1.close();
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class ConcurrentDeliveryCancelTest method testConcurrentCancels.
@Test
@BMRules(rules = { @BMRule(name = "enterCancel-holdThere", targetClass = "org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl", targetMethod = "close", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ConcurrentDeliveryCancelTest.enterCancel();") })
public void testConcurrentCancels() throws Exception {
System.out.println(server.getConfiguration().getJournalLocation().toString());
server.getAddressSettingsRepository().clear();
AddressSettings settings = new AddressSettings();
settings.setMaxDeliveryAttempts(-1);
server.getAddressSettingsRepository().addMatch("#", settings);
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
cf.setReconnectAttempts(0);
cf.setRetryInterval(10);
System.out.println(".....");
for (ServerSession srvSess : server.getSessions()) {
System.out.println(srvSess);
}
String queueName = RandomUtil.randomString();
Queue queue = createQueue(queueName);
int numberOfMessages = 10000;
{
Connection connection = cf.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < numberOfMessages; i++) {
TextMessage msg = session.createTextMessage("message " + i);
msg.setIntProperty("i", i);
producer.send(msg);
}
session.commit();
connection.close();
}
for (int i = 0; i < 100; i++) {
XAConnectionFactory xacf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
final XAConnection connection = xacf.createXAConnection();
final XASession theSession = connection.createXASession();
((ActiveMQSession) theSession).getCoreSession().addMetaData("theSession", "true");
connection.start();
final MessageConsumer consumer = theSession.createConsumer(queue);
XidImpl xid = newXID();
theSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
// I'm setting a small timeout just because I'm lazy to call end myself
theSession.getXAResource().setTransactionTimeout(1);
for (int msg = 0; msg < 11; msg++) {
Assert.assertNotNull(consumer.receiveNoWait());
}
System.out.println(".....");
final List<ServerSession> serverSessions = new LinkedList<>();
// We will force now the failure simultaneously from several places
for (ServerSession srvSess : server.getSessions()) {
if (srvSess.getMetaData("theSession") != null) {
System.out.println(srvSess);
serverSessions.add(srvSess);
}
}
// from Transactional reaper
resetLatches(2);
List<Thread> threads = new LinkedList<>();
threads.add(new Thread("ConsumerCloser") {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " closing consumer");
consumer.close();
System.out.println(Thread.currentThread().getName() + " closed consumer");
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(new Thread("SessionCloser") {
@Override
public void run() {
for (ServerSession sess : serverSessions) {
System.out.println("Thread " + Thread.currentThread().getName() + " starting");
try {
// A session.close could sneak in through failover or some other scenarios.
// a call to RemotingConnection.fail wasn't replicating the issue.
// I needed to call Session.close() directly to replicate what was happening in production
sess.close(true);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread " + Thread.currentThread().getName() + " done");
}
}
});
for (Thread t : threads) {
t.start();
}
Assert.assertTrue(latchEnter.await(10, TimeUnit.MINUTES));
latchFlag.countDown();
for (Thread t : threads) {
t.join(5000);
Assert.assertFalse(t.isAlive());
}
connection.close();
}
Connection connection = cf.createConnection();
try {
connection.setClientID("myID");
// I am too lazy to call end on all the transactions
Thread.sleep(5000);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
HashMap<Integer, AtomicInteger> mapCount = new HashMap<>();
while (true) {
TextMessage message = (TextMessage) consumer.receiveNoWait();
if (message == null) {
break;
}
Integer value = message.getIntProperty("i");
AtomicInteger count = mapCount.get(value);
if (count == null) {
count = new AtomicInteger(0);
mapCount.put(message.getIntProperty("i"), count);
}
count.incrementAndGet();
}
boolean failed = false;
for (int i = 0; i < numberOfMessages; i++) {
AtomicInteger count = mapCount.get(i);
if (count == null) {
System.out.println("Message " + i + " not received");
failed = true;
} else if (count.get() > 1) {
System.out.println("Message " + i + " received " + count.get() + " times");
failed = true;
}
}
Assert.assertFalse("test failed, look at the system.out of the test for more information", failed);
} finally {
connection.close();
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class JmsProducerTest method multipleSendsUsingSetters.
@Test
public void multipleSendsUsingSetters() throws Exception {
server.createQueue(SimpleString.toSimpleString("q1"), RoutingType.ANYCAST, SimpleString.toSimpleString("q1"), null, true, false);
Queue q1 = context.createQueue("q1");
context.createProducer().setProperty("prop1", 1).setProperty("prop2", 2).send(q1, "Text1");
context.createProducer().setProperty("prop1", 3).setProperty("prop2", 4).send(q1, "Text2");
for (int i = 0; i < 100; i++) {
context.createProducer().send(q1, "Text" + i);
}
ActiveMQSession sessionUsed = (ActiveMQSession) (((ActiveMQJMSContext) context).getUsedSession());
ClientSessionImpl coreSession = (ClientSessionImpl) sessionUsed.getCoreSession();
// JMSConsumer is supposed to cache the producer, each call to createProducer is supposed to always return the same producer
assertEquals(1, coreSession.cloneProducers().size());
JMSConsumer consumer = context.createConsumer(q1);
TextMessage text = (TextMessage) consumer.receive(5000);
assertNotNull(text);
assertEquals("Text1", text.getText());
assertEquals(1, text.getIntProperty("prop1"));
assertEquals(2, text.getIntProperty("prop2"));
text = (TextMessage) consumer.receive(5000);
assertNotNull(text);
assertEquals("Text2", text.getText());
assertEquals(3, text.getIntProperty("prop1"));
assertEquals(4, text.getIntProperty("prop2"));
for (int i = 0; i < 100; i++) {
assertEquals("Text" + i, consumer.receiveBody(String.class, 1000));
}
consumer.close();
context.close();
}
Aggregations