Java Code Examples for javax.jms.TopicPublisher#publish()
The following examples show how to use
javax.jms.TopicPublisher#publish() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JmsTopicPublisherTest.java From qpid-jms with Apache License 2.0 | 6 votes |
@Test(timeout = 10000) public void testPublishMessageOnProvidedTopicWhenNotAnonymous() throws Exception { Topic topic = session.createTopic(getTestName()); TopicPublisher publisher = session.createPublisher(topic); Message message = session.createMessage(); try { publisher.publish(session.createTopic(getTestName() + "1"), message); fail("Should throw UnsupportedOperationException"); } catch (UnsupportedOperationException uoe) {} try { publisher.publish((Topic) null, message); fail("Should throw InvalidDestinationException"); } catch (InvalidDestinationException ide) {} }
Example 2
Source File: TracingMessageProducer.java From brave with Apache License 2.0 | 6 votes |
@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(); } }
Example 3
Source File: JmsTopicPublisherTest.java From qpid-jms with Apache License 2.0 | 6 votes |
@Test(timeout = 10000) public void testPublishMessageWithOptionsOnProvidedTopicWhenNotAnonymous() throws Exception { Topic topic = session.createTopic(getTestName()); TopicPublisher publisher = session.createPublisher(topic); Message message = session.createMessage(); try { publisher.publish(session.createTopic(getTestName() + "1"), message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); fail("Should throw UnsupportedOperationException"); } catch (UnsupportedOperationException uoe) {} try { publisher.publish((Topic) null, message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); fail("Should throw InvalidDestinationException"); } catch (InvalidDestinationException ide) {} }
Example 4
Source File: AcknowledgementTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
/** * 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); }
Example 5
Source File: AcknowledgementTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
/** * Topics shouldn't hold on to messages if there are no subscribers */ @Test public void testPersistentMessagesForTopicDropped() throws Exception { TopicConnection topicConn = createTopicConnection(); TopicSession sess = topicConn.createTopicSession(true, 0); TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1); pub.setDeliveryMode(DeliveryMode.PERSISTENT); Message m = sess.createTextMessage("testing123"); pub.publish(m); sess.commit(); topicConn.close(); checkEmpty(ActiveMQServerTestCase.topic1); }
Example 6
Source File: TracingMessageProducer.java From brave with Apache License 2.0 | 6 votes |
@Override public void publish(Topic topic, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { checkTopicPublisher(); TopicPublisher tp = (TopicPublisher) delegate; Span span = createAndStartProducerSpan(message, destination(message)); SpanInScope ws = tracer.withSpanInScope(span); Throwable error = null; try { tp.publish(topic, message, deliveryMode, priority, timeToLive); } catch (Throwable t) { propagateIfFatal(t); error = t; throw t; } finally { if (error != null) span.error(error); span.finish(); ws.close(); } }
Example 7
Source File: TracingMessageProducer.java From brave with Apache License 2.0 | 6 votes |
@Override public void publish(Message message, int deliveryMode, int priority, long timeToLive) 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, deliveryMode, priority, timeToLive); } catch (Throwable t) { propagateIfFatal(t); error = t; throw t; } finally { if (error != null) span.error(error); span.finish(); ws.close(); } }
Example 8
Source File: JMSSample.java From WeEvent with Apache License 2.0 | 6 votes |
private static void publish() throws JMSException { // get topic connection TopicConnectionFactory connectionFactory = new WeEventConnectionFactory(defaultBrokerUrl); TopicConnection connection = connectionFactory.createTopicConnection(); // start connection connection.start(); // create session TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // create topic Topic topic = session.createTopic(topicName); // create publisher TopicPublisher publisher = session.createPublisher(topic); // send message BytesMessage msg = session.createBytesMessage(); msg.writeBytes(("hello WeEvent").getBytes(StandardCharsets.UTF_8)); publisher.publish(msg); System.out.print("send done."); connection.close(); }
Example 9
Source File: TopicWildcardTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
private void assertNullWithPublishSubscribeForTopics(String publishTopicName, String subscribeTopicName) throws Exception { int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder("admin", "admin", "localhost", port) .withTopic(publishTopicName) .withTopic(subscribeTopicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(subscribeTopicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); TopicSession publisherSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic publisherDestination = (Topic) initialContext.lookup(publishTopicName); TopicPublisher publisher = publisherSession.createPublisher(publisherDestination); for (int i = 0; i < numberOfMessages; i++) { publisher.publish(publisherSession.createTextMessage("Test message " + i)); } publisherSession.close(); Message message = subscriber.receive(1000); Assert.assertNull(message, "A message was received where no message was expected"); subscriberSession.close(); connection.close(); }
Example 10
Source File: SimpleOpenWireTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Test public void testTempTopicDelete() throws Exception { connection.start(); TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryTopic tempTopic = topicSession.createTemporaryTopic(); ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection(); try { TopicSession newTopicSession = newConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher publisher = newTopicSession.createPublisher(tempTopic); // need to wait here because the ActiveMQ client's temp destination map is updated asynchronously, not waiting can introduce a race assertTrue(Wait.waitFor(() -> newConn.activeTempDestinations.size() == 1, 2000, 100)); TextMessage msg = newTopicSession.createTextMessage("Test Message"); publisher.publish(msg); try { TopicSubscriber consumer = newTopicSession.createSubscriber(tempTopic); fail("should have gotten exception but got consumer: " + consumer); } catch (JMSException ex) { //correct } connection.close(); try { Message newMsg = newTopicSession.createMessage(); publisher.publish(newMsg); } catch (JMSException e) { //ok } } finally { newConn.close(); } }
Example 11
Source File: TopicWildcardTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
private void assertNotNullWithPublishSubscribeForTopics(String publishTopicName, String subscribeTopicName) throws Exception { int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder("admin", "admin", "localhost", port) .withTopic(publishTopicName) .withTopic(subscribeTopicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(subscribeTopicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); TopicSession publisherSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic publisherDestination = (Topic) initialContext.lookup(publishTopicName); TopicPublisher publisher = publisherSession.createPublisher(publisherDestination); for (int i = 0; i < numberOfMessages; i++) { publisher.publish(publisherSession.createTextMessage("Test message " + i)); } publisherSession.close(); for (int i = 0; i < numberOfMessages; i++) { Message message = subscriber.receive(1000); Assert.assertNotNull(message, "Message #" + i + " was not received"); } subscriberSession.close(); connection.close(); }
Example 12
Source File: TopicSubscriberTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
@Parameters({ "broker-port"}) @Test public void testSubscriberPublisher(String port) throws Exception { String topicName = "MyTopic1"; int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder("admin", "admin", "localhost", port) .withTopic(topicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); // Initialize subscriber TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(topicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); // publish 100 messages TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher producer = producerSession.createPublisher(subscriberDestination); for (int i = 0; i < numberOfMessages; i++) { producer.publish(producerSession.createTextMessage("Test message " + i)); } producerSession.close(); for (int i = 0; i < numberOfMessages; i++) { Message message = subscriber.receive(1000); Assert.assertNotNull(message, "Message #" + i + " was not received"); } connection.close(); }
Example 13
Source File: JmsTopicPublisherTest.java From qpid-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 10000) public void testPublishMessageWithDeliveryOptions() throws Exception { Topic topic = session.createTopic(getTestName()); TopicPublisher publisher = session.createPublisher(topic); Message message = session.createMessage(); publisher.publish(message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage(); assertNotNull(envelope); message = envelope.getMessage(); Destination destination = message.getJMSDestination(); assertEquals(topic, destination); }
Example 14
Source File: PooledTopicPublisherTest.java From pooled-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testJmsPoolConnectionFactory() throws Exception { ActiveMQTopic topic = new ActiveMQTopic("test"); pcf = createPooledConnectionFactory(); connection = (TopicConnection) pcf.createConnection(); TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher publisher = session.createPublisher(topic); publisher.publish(session.createMessage()); }
Example 15
Source File: JmsPoolTopicPublisherTest.java From pooled-jms with Apache License 2.0 | 5 votes |
@Test public void testPublishToTopicFailsIfNotAnonymousPublisher() throws JMSException { JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection(); TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTemporaryTopic(); TopicPublisher publisher = session.createPublisher(topic); try { publisher.publish(session.createTemporaryTopic(), session.createTextMessage()); fail("Should not be able to send to alternate destination"); } catch (UnsupportedOperationException ex) {} }
Example 16
Source File: TopicLocalTransactionCommitTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"}) @Test(expectedExceptions = javax.jms.IllegalStateException.class, expectedExceptionsMessageRegExp = ".*Session is not transacted") public void testCommitOnNonTransactionTopicSession(String port, String adminUsername, String adminPassword, String brokerHostname) throws NamingException, JMSException { String topicName = "testCommitOnNonTransactionTopicSession"; int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port) .withTopic(topicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); // initialize subscriber TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(topicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); // publish 100 messages TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher producer = producerSession.createPublisher(subscriberDestination); for (int i = 0; i < numberOfMessages; i++) { producer.publish(producerSession.createTextMessage("Test message " + i)); } try { // commit all publish messages producerSession.commit(); Message message = subscriber.receive(1000); Assert.assertNull(message, "Messages should not receive message after calling commit on " + "non transaction channel"); } catch (JMSException e) { //catch exception and re-throw it since we need the connection to be closed throw e; } finally { producerSession.close(); subscriberSession.close(); connection.close(); } }
Example 17
Source File: BDBUpgradeTest.java From qpid-broker-j with Apache License 2.0 | 4 votes |
/** * Test that the selector applied to the DurableSubscription was successfully * transferred to the new store, and functions as expected with continued use * by monitoring message count while sending new messages to the topic and then * consuming them. */ @Test public void testSelectorDurability() throws Exception { TopicConnection connection = getTopicConnection(); try { connection.start(); TopicSession session = connection.createTopicSession(true, Session.SESSION_TRANSACTED); Topic topic = session.createTopic(SELECTOR_TOPIC_NAME); TopicPublisher publisher = session.createPublisher(topic); int index = ThreadLocalRandom.current().nextInt(); Message messageA = session.createTextMessage("A"); messageA.setIntProperty("ID", index); messageA.setStringProperty("testprop", "false"); publisher.publish(messageA); Message messageB = session.createTextMessage("B"); messageB.setIntProperty("ID", index); messageB.setStringProperty("testprop", "true"); publisher.publish(messageB); session.commit(); TopicSubscriber subscriber = session.createDurableSubscriber(topic, SELECTOR_SUB_NAME, "testprop='true'", false); Message migrated = subscriber.receive(getReceiveTimeout()); assertThat("Failed to receive migrated message", migrated, is(notNullValue())); Message received = subscriber.receive(getReceiveTimeout()); session.commit(); assertThat("Failed to receive published message", received, is(notNullValue())); assertThat("Message is not Text message", received, is(instanceOf(TextMessage.class))); assertThat("Unexpected text", ((TextMessage) received).getText(), is(equalTo("B"))); assertThat("Unexpected index", received.getIntProperty("ID"), is(equalTo(index))); session.close(); } finally { connection.close(); } }
Example 18
Source File: TopicLocalTransactionRollbackTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"}) @Test(expectedExceptions = javax.jms.IllegalStateException.class, expectedExceptionsMessageRegExp = ".*Session is not transacted") public void testRollbackOnNonTransactionTopicSession(String port, String adminUsername, String adminPassword, String brokerHostname) throws NamingException, JMSException { String topicName = "testRollbackOnNonTransactionTopicSession"; int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port) .withTopic(topicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); // Initialize subscriber TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(topicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); // publish 100 messages TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher producer = producerSession.createPublisher(subscriberDestination); for (int i = 0; i < numberOfMessages; i++) { producer.publish(producerSession.createTextMessage("Test message " + i)); } try { // commit all publish messages producerSession.rollback(); Message message = subscriber.receive(1000); Assert.assertNull(message, "Messages should not receive message after calling rollback on " + "non transaction channel"); } catch (JMSException e) { throw e; } finally { producerSession.close(); subscriberSession.close(); connection.close(); } }
Example 19
Source File: TopicLocalTransactionRollbackTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"}) @Test public void testPublisherRollbackTransaction(String port, String adminUsername, String adminPassword, String brokerHostname) throws NamingException, JMSException { String topicName = "testPublisherRollbackTransaction"; int numberOfMessages = 100; InitialContext initialContext = ClientHelper .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port) .withTopic(topicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); // initialize subscriber TopicSession subscriberSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED); Topic subscriberDestination = (Topic) initialContext.lookup(topicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); // publish 100 messages TopicSession producerSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED); TopicPublisher producer = producerSession.createPublisher(subscriberDestination); for (int i = 0; i < numberOfMessages; i++) { producer.publish(producerSession.createTextMessage("Test message " + i)); } // rollback all publish messages producerSession.rollback(); // Consume published messages Message message = subscriber.receive(1000); Assert.assertNull(message, "Messages should not receive upon publisher rollback"); producerSession.close(); subscriberSession.close(); connection.close(); }
Example 20
Source File: TopicMessagesOrderTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"}) @Test public void test1338TopicMessagesOrderSingleSubscriber(String port, String adminUsername, String adminPassword, String brokerHostname) throws NamingException, JMSException { String topicName = "test1338TopicMessagesOrderSingleSubscriber"; List<String> subscriberOneMessages = new ArrayList<>(); int numberOfMessages = 1338; InitialContext initialContext = ClientHelper .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port) .withTopic(topicName) .build(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY); TopicConnection connection = connectionFactory.createTopicConnection(); connection.start(); // Initialize subscriber TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic subscriberDestination = (Topic) initialContext.lookup(topicName); TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination); // publish 1338 messages TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher producer = producerSession.createPublisher(subscriberDestination); for (int i = 0; i < numberOfMessages; i++) { producer.publish(producerSession.createTextMessage(String.valueOf(i))); } producerSession.close(); for (int i = 0; i < numberOfMessages; i++) { TextMessage message = (TextMessage) subscriber.receive(1000); Assert.assertNotNull(message, "Message #" + i + " was not received"); subscriberOneMessages.add(message.getText()); } subscriberSession.close(); connection.close(); // verify order is preserved boolean isOrderPreserved = true; for (int i = 0; i < numberOfMessages; i++) { if (!(i == Integer.parseInt(subscriberOneMessages.get(i)))) { isOrderPreserved = false; break; } } Assert.assertTrue(isOrderPreserved, "Topic messages order not preserved for single subscriber."); }