当前位置: 首页>>代码示例>>Java>>正文


Java TopicPublisher.send方法代码示例

本文整理汇总了Java中javax.jms.TopicPublisher.send方法的典型用法代码示例。如果您正苦于以下问题:Java TopicPublisher.send方法的具体用法?Java TopicPublisher.send怎么用?Java TopicPublisher.send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.jms.TopicPublisher的用法示例。


在下文中一共展示了TopicPublisher.send方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testSendAndReceiveOnTopic

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSendAndReceiveOnTopic() throws Exception {
   Connection connection = createConnection("myClientId");

   try {
      TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Topic topic = session.createTopic(getTopicName());
      TopicSubscriber consumer = session.createSubscriber(topic);
      TopicPublisher producer = session.createPublisher(topic);

      TextMessage message = session.createTextMessage("test-message");
      producer.send(message);

      producer.close();
      connection.start();

      message = (TextMessage) consumer.receive(1000);

      assertNotNull(message);
      assertNotNull(message.getText());
      assertEquals("test-message", message.getText());
   } finally {
      connection.close();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:26,代码来源:JMSTopicConsumerTest.java

示例2: testSendingSameMessage

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
public void testSendingSameMessage() throws Exception
{
    AMQConnection conn = (AMQConnection) getConnection("guest", "guest");
    TopicSession session = conn.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
    TemporaryTopic topic = session.createTemporaryTopic();
    assertNotNull(topic);
    TopicPublisher producer = session.createPublisher(topic);
    MessageConsumer consumer = session.createConsumer(topic);
    conn.start();
    TextMessage sentMessage = session.createTextMessage("Test Message");
    producer.send(sentMessage);
    session.commit();
    TextMessage receivedMessage = (TextMessage) consumer.receive(2000);
    assertNotNull(receivedMessage);
    assertEquals(sentMessage.getText(), receivedMessage.getText());
    producer.send(sentMessage);
    session.commit();
    receivedMessage = (TextMessage) consumer.receive(2000);
    assertNotNull(receivedMessage);
    assertEquals(sentMessage.getText(), receivedMessage.getText());
    session.commit();
    conn.close();

}
 
开发者ID:wso2,项目名称:andes,代码行数:25,代码来源:TopicSessionTest.java

示例3: sendEnqueueMessage

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
public void sendEnqueueMessage() {
    ServiceLocator locator = ServiceLocatorFactory.getLocator();
    final TopicConnectionFactory factory = (TopicConnectionFactory) locator.lookup(DEFAULT_QUEUE_CONN_FACTORY);
    final Topic topic = (Topic) locator.lookup(FileManagementMDB.QUEUE_JNDI_NAME);
    TopicConnection connection = null;
    TopicSession session = null;
    TopicPublisher publisher = null;
    try {
        connection = factory.createTopicConnection();
        session = connection.createTopicSession(true, 0);
        publisher = session.createPublisher(topic);
        
        final Message message = session.createTextMessage("enqueue");
        publisher.send(message);
    } catch (JMSException e) {
        LOG.error("Couldn't submit job to JMS", e);
    } finally {
        close(publisher);
        close(session);
        close(connection);
    }
}
 
开发者ID:NCIP,项目名称:caarray,代码行数:26,代码来源:JobMessageSenderImpl.java

示例4: testSendAndReceiveOnAutoCreatedTopic

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSendAndReceiveOnAutoCreatedTopic() throws Exception {
   Connection connection = createConnection("myClientId");
   String topicName = UUID.randomUUID().toString();
   SimpleString simpleTopicName = SimpleString.toSimpleString(topicName);

   try {
      TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Topic topic = session.createTopic(topicName);
      TopicPublisher producer = session.createPublisher(topic);

      TextMessage message = session.createTextMessage("test-message");
      // this will auto-create the address, but not the subscription queue
      producer.send(message);

      assertNotNull(server.getAddressInfo(simpleTopicName));
      assertEquals(RoutingType.MULTICAST, server.getAddressInfo(simpleTopicName).getRoutingType());
      assertTrue(server.getAddressInfo(simpleTopicName).isAutoCreated());
      assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());

      // this will auto-create the subscription queue
      TopicSubscriber consumer = session.createSubscriber(topic);
      assertFalse(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
      producer.send(message);

      producer.close();
      connection.start();

      message = (TextMessage) consumer.receive(1000);

      assertNotNull(message);
      assertNotNull(message.getText());
      assertEquals("test-message", message.getText());
      consumer.close();
      assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
   } finally {
      connection.close();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:40,代码来源:JMSTopicConsumerTest.java

示例5: testSendWithMultipleReceiversOnTopic

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@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();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:33,代码来源:JMSTopicConsumerTest.java

示例6: testPositiveJMSSelectorConsumerProducer

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testPositiveJMSSelectorConsumerProducer(String port,
                                                    String adminUsername,
                                                    String adminPassword,
                                                    String brokerHostname) throws NamingException, JMSException {
    String queueName = "testPositiveJMSSelectorConsumerProducer";
    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(queueName)
            .build();


    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = (Topic) initialContext.lookup(queueName);

    // Subscribe with a selector
    String propertyName = "MyProperty";
    String propertyValue = "propertyValue";
    String jmsPropertySelector = propertyName + " = '" + propertyValue + "'";
    TopicSubscriber consumer = subscriberSession.createSubscriber(topic, jmsPropertySelector, false);

    // publish messages with property
    TopicSession producerSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(topic);

    int numberOfMessages = 100;
    for (int i = 0; i < numberOfMessages; i++) {
        TextMessage textMessage = producerSession.createTextMessage("Test message " + i);
        textMessage.setStringProperty(propertyName, propertyValue);
        producer.send(textMessage);
    }

    // consume messages
    for (int i = 0; i < numberOfMessages; i++) {
        Message message = consumer.receive(1000);
        Assert.assertNotNull(message, "Message #" + i + " was not received");
    }

    producerSession.close();
    connection.close();
}
 
开发者ID:wso2,项目名称:message-broker,代码行数:48,代码来源:JMSSelectorTest.java

示例7: testNegativeJMSSelectorConsumerProducer

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testNegativeJMSSelectorConsumerProducer(String port,
                                                    String adminUsername,
                                                    String adminPassword,
                                                    String brokerHostname) throws NamingException, JMSException {
    String queueName = "testNegativeJMSSelectorConsumerProducer";
    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(queueName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = (Topic) initialContext.lookup(queueName);

    // Subscribe with a selector
    String propertyName = "MyProperty";
    String propertyValue = "propertyValue";
    String jmsPropertySelector = propertyName + " = '" + propertyValue + "'";
    TopicSubscriber consumer = subscriberSession.createSubscriber(topic, jmsPropertySelector, false);

    // publish messages with property
    TopicSession producerSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(topic);

    // Send messages with a different property value
    int numberOfMessages = 100;
    for (int i = 0; i < numberOfMessages; i++) {
        TextMessage textMessage = producerSession.createTextMessage("Test message " + i);
        textMessage.setStringProperty(propertyName, propertyValue + "-1");
        producer.send(textMessage);
    }

    // consume messages
    Message message = consumer.receive(100);
    Assert.assertNull(message, "Message received. Shouldn't receive any messages.");

    producerSession.close();
    connection.close();
}
 
开发者ID:wso2,项目名称:message-broker,代码行数:46,代码来源:JMSSelectorTest.java

示例8: testTopic

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
/**
 *
 */
@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();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:69,代码来源:ObjectMessageDeliveryTest.java


注:本文中的javax.jms.TopicPublisher.send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。