use of javax.jms.TopicPublisher in project brave by openzipkin.
the class TracingMessageProducer method publish.
@Override
public void publish(Message message) throws JMSException {
checkTopicPublisher();
TopicPublisher tp = (TopicPublisher) delegate;
Span span = createAndStartProducerSpan(message, destination(message));
SpanInScope ws = tracer.withSpanInScope(span);
Throwable error = null;
try {
tp.publish(message);
} catch (Throwable t) {
propagateIfFatal(t);
error = t;
throw t;
} finally {
if (error != null)
span.error(error);
span.finish();
ws.close();
}
}
use of javax.jms.TopicPublisher in project qpid-broker-j by apache.
the class DurableSubscribtionTest method testChangeSubscriberToHaveSelector.
/**
* create and register a durable subscriber without a message selector and then unsubscribe it
* create and register a durable subscriber with a message selector and then close it
* restart the broker
* send matching and non matching messages
* recreate and register the durable subscriber with a message selector
* verify only the matching messages are received
*/
@Test
public void testChangeSubscriberToHaveSelector() throws Exception {
assumeThat(getBrokerAdmin().supportsRestart(), is(true));
final String subscriptionName = getTestName() + "_sub";
Topic topic = createTopic(getTestName());
String testClientId = "testClientId";
TopicConnection subscriberConnection = (TopicConnection) getConnectionBuilder().setClientId(testClientId).build();
try {
TopicSession session = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName);
TopicPublisher publisher = session.createPublisher(topic);
publisher.send(session.createTextMessage("Message1"));
publisher.send(session.createTextMessage("Message2"));
subscriberConnection.start();
Message receivedMessage = subscriber.receive(getReceiveTimeout());
assertTrue(receivedMessage instanceof TextMessage);
assertEquals("Unexpected message content", "Message1", ((TextMessage) receivedMessage).getText());
subscriber.close();
session.close();
} finally {
subscriberConnection.close();
}
// create and register a durable subscriber with a message selector and then close it
TopicConnection subscriberConnection2 = (TopicConnection) getConnectionBuilder().setClientId(testClientId).build();
try {
TopicSession session = subscriberConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName, "testprop='true'", false);
TopicPublisher publisher = session.createPublisher(topic);
TextMessage message = session.createTextMessage("Message3");
message.setStringProperty("testprop", "false");
publisher.send(message);
message = session.createTextMessage("Message4");
message.setStringProperty("testprop", "true");
publisher.send(message);
subscriberConnection2.start();
Message receivedMessage = subscriber.receive(getReceiveTimeout());
assertTrue(receivedMessage instanceof TextMessage);
assertEquals("Unexpected message content", "Message4", ((TextMessage) receivedMessage).getText());
subscriber.close();
session.close();
} finally {
subscriberConnection2.close();
}
getBrokerAdmin().restart();
TopicConnection publisherConnection = getTopicConnection();
try {
TopicSession session = publisherConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session.createPublisher(topic);
for (int i = 0; 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();
}
TopicConnection subscriberConnection3 = (TopicConnection) getConnectionBuilder().setClientId(testClientId).build();
try {
TopicSession session = (TopicSession) subscriberConnection3.createSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName, "testprop='true'", false);
subscriberConnection3.start();
for (int i = 0; i < 5; i++) {
Message message = subscriber.receive(2000);
if (message == null) {
fail(String.format("Message '%d' was not received", i));
} else {
assertTrue(String.format("Received message %d with not matching selector", i), message.getStringProperty("testprop").equals("true"));
}
}
subscriber.close();
session.unsubscribe(subscriptionName);
session.close();
} finally {
subscriberConnection3.close();
}
}
use of javax.jms.TopicPublisher in project qpid-broker-j by apache.
the class TopicSubscriberTest method messageDeliveredToAllSubscribers.
@Test
public void messageDeliveredToAllSubscribers() throws Exception {
Topic topic = createTopic(getTestName());
final TopicConnection connection = getTopicConnection();
try {
final TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
final TopicPublisher producer = session.createPublisher(topic);
final TopicSubscriber subscriber1 = session.createSubscriber(topic);
assertEquals("Unexpected subscriber1 topic", topic.getTopicName(), subscriber1.getTopic().getTopicName());
final TopicSubscriber subscriber2 = session.createSubscriber(topic);
assertEquals("Unexpected subscriber2 topic", topic.getTopicName(), subscriber2.getTopic().getTopicName());
connection.start();
String messageText = "Test Message";
producer.send(session.createTextMessage(messageText));
final Message subscriber1Message = subscriber1.receive(getReceiveTimeout());
final Message subscriber2Message = subscriber2.receive(getReceiveTimeout());
assertTrue("TextMessage should be received by subscriber1", subscriber1Message instanceof TextMessage);
assertEquals(messageText, ((TextMessage) subscriber1Message).getText());
assertTrue("TextMessage should be received by subscriber2", subscriber2Message instanceof TextMessage);
assertEquals(messageText, ((TextMessage) subscriber2Message).getText());
} finally {
connection.close();
}
}
use of javax.jms.TopicPublisher in project qpid-broker-j by apache.
the class DurableSubscribtionTest method testMessageSelectorRecoveredOnBrokerRestart.
/**
* create and register a durable subscriber with a message selector and then close it
* crash the broker
* create a publisher and send 5 right messages and 5 wrong messages
* recreate the durable subscriber and check we receive the 5 expected messages
*/
@Test
public void testMessageSelectorRecoveredOnBrokerRestart() throws Exception {
assumeThat(getBrokerAdmin().supportsRestart(), is(true));
final Topic topic = createTopic(getTestName());
String clientId = "testClientId";
String subscriptionName = getTestName() + "_sub";
TopicConnection subscriberConnection = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName, "testprop='true'", false);
subscriberConnection.start();
subscriber.close();
session.close();
} finally {
subscriberConnection.close();
}
getBrokerAdmin().restart();
TopicConnection connection = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session.createPublisher(topic);
for (int i = 0; i < 10; i++) {
Message message = session.createMessage();
message.setStringProperty("testprop", String.valueOf(i % 2 == 0));
publisher.publish(message);
}
publisher.close();
session.close();
} finally {
connection.close();
}
TopicConnection subscriberConnection2 = (TopicConnection) getConnectionBuilder().setClientId(clientId).build();
try {
TopicSession session = subscriberConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriptionName, "testprop='true'", false);
subscriberConnection2.start();
for (int i = 0; i < 5; i++) {
Message message = subscriber.receive(1000);
if (message == null) {
fail(String.format("Message '%d' was received", i));
} else {
assertTrue(String.format("Received message %d with not matching selector", i), message.getStringProperty("testprop").equals("true"));
}
}
subscriber.close();
session.unsubscribe(subscriptionName);
} finally {
subscriberConnection2.close();
}
}
use of javax.jms.TopicPublisher in project qpid-broker-j by apache.
the class TopicSessionTest method publisherGetDeliveryModeAfterConnectionClose.
@Test
public void publisherGetDeliveryModeAfterConnectionClose() throws Exception {
Topic topic = createTopic(getTestName());
TopicConnection connection = getTopicConnection();
try {
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session.createPublisher(topic);
connection.close();
try {
publisher.getDeliveryMode();
fail("Expected exception not thrown");
} catch (javax.jms.IllegalStateException e) {
// PASS
}
} finally {
connection.close();
}
}
Aggregations