use of javax.jms.TopicPublisher in project qpid-broker-j by apache.
the class DurableSubscribtionTest method testChangeSubscriberToHaveNoSelector.
/**
* create and register a durable subscriber with a message selector and then unsubscribe it
* create and register a durable subscriber without a message selector and then close it
* restart the broker
* send matching and non matching messages
* recreate and register the durable subscriber without a message selector
* verify ALL the sent messages are received
*/
@Test
public void testChangeSubscriberToHaveNoSelector() throws Exception {
assumeThat(getBrokerAdmin().supportsRestart(), is(true));
final String subscriptionName = getTestName() + "_sub";
Topic topic = createTopic(getTestName());
String clientId = "testClientId";
// create and register a durable subscriber with selector then unsubscribe it
TopicConnection durConnection = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = durConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName, "testprop='true'", false);
TopicPublisher publisher = session.createPublisher(topic);
TextMessage message = session.createTextMessage("Messag1");
message.setStringProperty("testprop", "false");
publisher.send(message);
message = session.createTextMessage("Message2");
message.setStringProperty("testprop", "true");
publisher.send(message);
message = session.createTextMessage("Message3");
message.setStringProperty("testprop", "true");
publisher.send(message);
durConnection.start();
Message receivedMessage = subscriber.receive(getReceiveTimeout());
assertTrue(receivedMessage instanceof TextMessage);
assertEquals("Unexpected message content", "Message2", ((TextMessage) receivedMessage).getText());
subscriber.close();
session.close();
} finally {
durConnection.close();
}
// create and register a durable subscriber without the message selector and then close it
TopicConnection subscriberConnection2 = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = subscriberConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName);
subscriberConnection2.start();
subscriber.close();
session.close();
} finally {
subscriberConnection2.close();
}
// send messages matching and not matching the original used selector
TopicConnection publisherConnection = getTopicConnection();
try {
TopicSession session = publisherConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session.createPublisher(topic);
for (int i = 1; i <= 10; i++) {
Message message = session.createMessage();
message.setStringProperty("testprop", String.valueOf(i % 2 == 0));
publisher.publish(message);
}
publisher.close();
session.close();
} finally {
publisherConnection.close();
}
getBrokerAdmin().restart();
TopicConnection subscriberConnection3 = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = (TopicSession) subscriberConnection3.createSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName);
subscriberConnection3.start();
for (int i = 1; i <= 10; i++) {
Message message = subscriber.receive(2000);
if (message == null) {
fail(String.format("Message %d was not received", i));
}
}
subscriber.close();
session.unsubscribe(subscriptionName);
session.close();
} finally {
subscriberConnection3.close();
}
}
use of javax.jms.TopicPublisher in project activemq-artemis by apache.
the class JMSTopicConsumerTest method testSendWithMultipleReceiversOnTopic.
@Test(timeout = 60000)
public void testSendWithMultipleReceiversOnTopic() throws Exception {
Connection connection = createConnection();
try {
TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(getTopicName());
TopicSubscriber consumer1 = session.createSubscriber(topic);
TopicSubscriber consumer2 = session.createSubscriber(topic);
TopicPublisher producer = session.createPublisher(topic);
TextMessage message = session.createTextMessage("test-message");
producer.send(message);
producer.close();
connection.start();
message = (TextMessage) consumer1.receive(1000);
assertNotNull(message);
assertNotNull(message.getText());
assertEquals("test-message", message.getText());
message = (TextMessage) consumer2.receive(1000);
assertNotNull(message);
assertNotNull(message.getText());
assertEquals("test-message", message.getText());
} finally {
connection.close();
}
}
use of javax.jms.TopicPublisher in project activemq-artemis by apache.
the class ObjectMessageDeliveryTest method testTopic.
/**
*/
@Test
public void testTopic() throws Exception {
TopicConnection conn = getTopicConnectionFactory().createTopicConnection();
try {
TopicSession s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = s.createPublisher(ActiveMQServerTestCase.topic1);
TopicSubscriber sub = s.createSubscriber(ActiveMQServerTestCase.topic1);
conn.start();
// Create 3 object messages with different bodies
TestObject to1 = new TestObject();
to1.text = "hello1";
TestObject to2 = new TestObject();
to1.text = "hello2";
TestObject to3 = new TestObject();
to1.text = "hello3";
ObjectMessage om1 = s.createObjectMessage();
om1.setObject(to1);
ObjectMessage om2 = s.createObjectMessage();
om2.setObject(to2);
ObjectMessage om3 = s.createObjectMessage();
om3.setObject(to3);
// send to topic
publisher.send(om1);
publisher.send(om2);
publisher.send(om3);
ObjectMessage rm1 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);
ObjectMessage rm2 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);
ObjectMessage rm3 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);
ProxyAssertSupport.assertNotNull(rm1);
TestObject ro1 = (TestObject) rm1.getObject();
ProxyAssertSupport.assertEquals(to1.text, ro1.text);
ProxyAssertSupport.assertNotNull(rm1);
TestObject ro2 = (TestObject) rm2.getObject();
ProxyAssertSupport.assertEquals(to2.text, ro2.text);
ProxyAssertSupport.assertNotNull(rm2);
TestObject ro3 = (TestObject) rm3.getObject();
ProxyAssertSupport.assertEquals(to3.text, ro3.text);
} finally {
if (conn != null) {
conn.close();
}
}
}
use of javax.jms.TopicPublisher in project activemq-artemis by apache.
the class AcknowledgementTest method testPersistentMessagesForTopicDropped2.
/**
* Topics shouldn't hold on to messages when the non-durable subscribers close
*/
@Test
public void testPersistentMessagesForTopicDropped2() throws Exception {
TopicConnection topicConn = createTopicConnection();
topicConn.start();
TopicSession sess = topicConn.createTopicSession(true, 0);
TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1);
TopicSubscriber sub = sess.createSubscriber(ActiveMQServerTestCase.topic1);
pub.setDeliveryMode(DeliveryMode.PERSISTENT);
Message m = sess.createTextMessage("testing123");
pub.publish(m);
sess.commit();
// receive but rollback
TextMessage m2 = (TextMessage) sub.receive(3000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals("testing123", m2.getText());
sess.rollback();
topicConn.close();
checkEmpty(ActiveMQServerTestCase.topic1);
}
use of javax.jms.TopicPublisher in project activemq-artemis by apache.
the class AcknowledgementTest method testRollbackRecover.
@Test
public void testRollbackRecover() throws Exception {
TopicConnection topicConn = createTopicConnection();
TopicSession sess = topicConn.createTopicSession(true, 0);
TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1);
TopicSubscriber cons = sess.createSubscriber(ActiveMQServerTestCase.topic1);
topicConn.start();
Message m = sess.createTextMessage("testing123");
pub.publish(m);
sess.commit();
TextMessage m2 = (TextMessage) cons.receive(3000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals("testing123", m2.getText());
sess.rollback();
m2 = (TextMessage) cons.receive(3000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals("testing123", m2.getText());
topicConn.close();
topicConn = createTopicConnection();
topicConn.start();
// test 2
TopicSession newsess = topicConn.createTopicSession(true, 0);
TopicPublisher newpub = newsess.createPublisher(ActiveMQServerTestCase.topic1);
TopicSubscriber newcons = newsess.createSubscriber(ActiveMQServerTestCase.topic1);
Message m3 = newsess.createTextMessage("testing456");
newpub.publish(m3);
newsess.commit();
TextMessage m4 = (TextMessage) newcons.receive(3000);
ProxyAssertSupport.assertNotNull(m4);
ProxyAssertSupport.assertEquals("testing456", m4.getText());
newsess.commit();
newpub.publish(m3);
newsess.commit();
TextMessage m5 = (TextMessage) newcons.receive(3000);
ProxyAssertSupport.assertNotNull(m5);
ProxyAssertSupport.assertEquals("testing456", m5.getText());
newsess.rollback();
TextMessage m6 = (TextMessage) newcons.receive(3000);
ProxyAssertSupport.assertNotNull(m6);
ProxyAssertSupport.assertEquals("testing456", m6.getText());
newsess.commit();
}
Aggregations