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


Java TopicPublisher.publish方法代码示例

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


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

示例1: publish

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
/**
 * @param topicConnection
 * @param chatTopic
 * @param userId
 * @throws JMSException
 * @throws IOException
 */
void publish(TopicConnection topicConnection, Topic chatTopic, String userId)
		throws JMSException, IOException {
	TopicSession tsession = topicConnection.createTopicSession(false,
			Session.AUTO_ACKNOWLEDGE);
	TopicPublisher topicPublisher = tsession.createPublisher(chatTopic);
	topicConnection.start();
	
	BufferedReader reader = new BufferedReader(new InputStreamReader(
			System.in));
	while (true) {
		String msgToSend = reader.readLine();
		if (msgToSend.equalsIgnoreCase("exit")) {
			topicConnection.close();
			System.exit(0);
		} else {

			TextMessage msg = (TextMessage) tsession.createTextMessage();
			msg.setText("\n["+userId + " : " + msgToSend+"]");
			topicPublisher.publish(msg);
		}
	}
}
 
开发者ID:Illusionist80,项目名称:SpringTutorial,代码行数:30,代码来源:BasicJMSChat.java

示例2: testPersistentMessagesForTopicDropped

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

示例3: testPersistentMessagesForTopicDropped2

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

示例4: testUnidentifiedProducer

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
public void testUnidentifiedProducer() throws Exception
{

    AMQConnection con =  (AMQConnection) getConnection("guest", "guest");
    AMQTopic topic = new AMQTopic(con,"MyTopic");
    TopicSession session1 = con.createTopicSession(false, AMQSession.NO_ACKNOWLEDGE);
    TopicPublisher publisher = session1.createPublisher(null);
    MessageConsumer consumer1 = session1.createConsumer(topic);
    con.start();
    publisher.publish(topic, session1.createTextMessage("Hello"));
    TextMessage m = (TextMessage) consumer1.receive(2000);
    assertNotNull(m);
    try
    {
        publisher.publish(session1.createTextMessage("Goodbye"));
        fail("Did not throw UnsupportedOperationException");
    }
    catch (UnsupportedOperationException e)
    {
        // PASS
    }
    con.close();
}
 
开发者ID:wso2,项目名称:andes,代码行数:24,代码来源:TopicPublisherTest.java

示例5: testPublishMessageOnProvidedTopicWhenNotAnonymous

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

示例6: testPublishMessageWithOptionsOnProvidedTopicWhenNotAnonymous

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

示例7: publishTextMsg2Topic

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
/**
 * Product message for assigned topic.
 * 
 * @param uri
 *            e.g.: tcp://3CNL12096:61616
 * @param queueName
 *            name of queue
 * @throws JMSException
 */
public static void publishTextMsg2Topic(String uri, String topicName, String text) throws JMSException {
	TopicConnectionFactory connectionFactory = null;
	TopicConnection connection = null;
	TopicSession session = null;
	TopicPublisher tp = null;
	try {
		connectionFactory = new ActiveMQConnectionFactory(uri);
		connection = connectionFactory.createTopicConnection();
		connection.start();
		session = connection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		tp = session.createPublisher(session.createTopic(topicName));
		tp.setDeliveryMode(DeliveryMode.PERSISTENT);
		tp.publish(session.createTextMessage(text));
		session.commit();
	} finally {
		closeQuietly(tp);
		closeQuietly(session);
		closeQuietly(connection);
	}
}
 
开发者ID:slimsymphony,项目名称:testgrid,代码行数:30,代码来源:MsgUtils.java

示例8: testPublishToTopicFailsIfNotAnonymousPublisher

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

示例9: testJmsPoolConnectionFactory

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testJmsPoolConnectionFactory() throws Exception {
    ActiveMQTopic topic = new ActiveMQTopic("test");
    pcf = new JmsPoolConnectionFactory();
    pcf.setConnectionFactory(new ActiveMQConnectionFactory(
        "vm://test?broker.persistent=false&broker.useJmx=false"));

    connection = (TopicConnection) pcf.createConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicPublisher publisher = session.createPublisher(topic);
    publisher.publish(session.createMessage());
}
 
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:13,代码来源:PooledTopicPublisherTest.java

示例10: testSubscriberPublisher

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

示例11: testTempTopicDelete

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@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);

      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();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:38,代码来源:SimpleOpenWireTest.java

示例12: main

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
public static void main(String[] args) {
	if (args.length != 2) {
		System.err.println("usage: JMS_Publisher <server> <port>");
	} else {
		TopicConnectionFactory connectionFactory;
		try {
			connectionFactory = ImaJmsFactory.createTopicConnectionFactory();
			ImaProperties imaProperties = (ImaProperties) connectionFactory;
			imaProperties.put("Server", args[0]);
			imaProperties.put("Port", args[1]);
			ImaJmsException[] exceptions = imaProperties.validate(false);
			if (exceptions != null) {
				for (ImaJmsException e : exceptions) {
					System.out.println(e.getMessage());
				}
				return;
			} else {
				TopicConnection connection = connectionFactory.createTopicConnection();
				String clientId = "JMS_Publisher";
				connection.setClientID(clientId);
				TopicSession session = connection.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
				Topic topic = session.createTopic("expirationTest");
				TopicPublisher publisher = session.createPublisher(topic);
				TextMessage message1 = session.createTextMessage("message1");
				publisher.publish(message1);
				TextMessage message2 = session.createTextMessage("message2");
				// Send this with an expiration time ten seconds from now
				publisher.publish(message2, DeliveryMode.PERSISTENT, 3, 10000);
				TextMessage message3 = session.createTextMessage("message3");
				publisher.publish(message3);
				System.out.println("Messages published");
			}
		} catch (JMSException e2) {
			e2.printStackTrace();
		}
	}
}
 
开发者ID:ibm-messaging,项目名称:messagesight-message-expiry,代码行数:38,代码来源:JMS_Publisher.java

示例13: run

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Override
public void run()
   {
       try
       {
           TopicPublisher publisher = getSession().createPublisher(topic);
           notifyStartup();
           
           startSynchro.waitFor();
           
           for (int n = 0 ; n < messageCount ; n++)
           {
               variableWait();
               
               int msgPriority = (priority != -1) ? priority : random.nextInt(10);
               
               Message message = msgFactory.createDummyMessage(messageSize);
               publisher.publish(message, deliveryMode, msgPriority, timeToLive);
               if (transacted)
                   getSession().commit();
           }
           
           publisher.close();
           //System.out.println(getName()+" thread complete ("+messageCount+" messages sent)");
       }
       catch (Throwable e)
       {
           inError = true;
           e.printStackTrace();
       }
   }
 
开发者ID:timewalker74,项目名称:ffmq,代码行数:32,代码来源:TopicPublisherThread.java

示例14: testUnsubscriptionAfterConnectionClose

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
public void testUnsubscriptionAfterConnectionClose() throws Exception
{
    AMQConnection con1 = (AMQConnection) getClientConnection("guest", "guest", "clientid");
    AMQTopic topic = new AMQTopic(con1, "MyTopic3");

    TopicSession session1 = con1.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
    TopicPublisher publisher = session1.createPublisher(topic);

    AMQConnection con2 = (AMQConnection) getClientConnection("guest", "guest", "clientid");
    TopicSession session2 = con2.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
    TopicSubscriber sub = session2.createDurableSubscriber(topic, "subscription0");

    con2.start();

    publisher.publish(session1.createTextMessage("Hello"));
    session1.commit();
    TextMessage tm = (TextMessage) sub.receive(2000);
    session2.commit();
    assertNotNull(tm);
    con2.close();
    publisher.publish(session1.createTextMessage("Hello2"));
    session1.commit();
    con2 =  (AMQConnection) getClientConnection("guest", "guest", "clientid");
    session2 = con2.createTopicSession(true, AMQSession.NO_ACKNOWLEDGE);
    sub = session2.createDurableSubscriber(topic, "subscription0");
    con2.start();
    tm = (TextMessage) sub.receive(2000);
    session2.commit();
    assertNotNull(tm);
    assertEquals("Hello2", tm.getText());
    session2.unsubscribe("subscription0");
    con1.close();
    con2.close();
}
 
开发者ID:wso2,项目名称:andes,代码行数:35,代码来源:TopicSessionTest.java

示例15: testPublishMessage

import javax.jms.TopicPublisher; //导入方法依赖的package包/类
@Test(timeout = 10000)
public void testPublishMessage() throws Exception {
    Topic topic = session.createTopic(getTestName());
    TopicPublisher publisher = session.createPublisher(topic);
    Message message = session.createMessage();
    publisher.publish(message);

    JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    message = envelope.getMessage();
    Destination destination = message.getJMSDestination();
    assertEquals(topic, destination);
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:14,代码来源:JmsTopicPublisherTest.java


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