本文整理匯總了Java中javax.jms.TopicSession.createTextMessage方法的典型用法代碼示例。如果您正苦於以下問題:Java TopicSession.createTextMessage方法的具體用法?Java TopicSession.createTextMessage怎麽用?Java TopicSession.createTextMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.TopicSession
的用法示例。
在下文中一共展示了TopicSession.createTextMessage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: publish
import javax.jms.TopicSession; //導入方法依賴的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);
}
}
}
示例2: publishMessagesToTopic
import javax.jms.TopicSession; //導入方法依賴的package包/類
/**
* To publish the messages to a topic.
*
* @throws JMSException JMS Exception.
* @throws InterruptedException Interrupted exception while waiting in between messages.
*/
public void publishMessagesToTopic(String topicName) throws JMSException, InterruptedException {
TopicConnection topicConnection = (TopicConnection) connectionFactory.createConnection();
topicConnection.start();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = topicSession.createTopic(topicName);
MessageProducer topicSender = topicSession.createProducer(destination);
topicSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int index = 0; index < 10; index++) {
String topicText = "Topic Message : " + (index + 1);
TextMessage topicMessage = topicSession.createTextMessage(topicText);
topicSender.send(topicMessage);
logger.info("Publishing " + topicText + " to topic " + topicName);
Thread.sleep(1000);
}
topicConnection.close();
topicSession.close();
topicSender.close();
}
示例3: testSendAndReceiveOnTopic
import javax.jms.TopicSession; //導入方法依賴的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();
}
}
示例4: testPersistentMessagesForTopicDropped
import javax.jms.TopicSession; //導入方法依賴的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);
}
示例5: testPersistentMessagesForTopicDropped2
import javax.jms.TopicSession; //導入方法依賴的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);
}
示例6: testSendingSameMessage
import javax.jms.TopicSession; //導入方法依賴的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();
}
示例7: sendEnqueueMessage
import javax.jms.TopicSession; //導入方法依賴的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);
}
}
示例8: testTempTopicDelete
import javax.jms.TopicSession; //導入方法依賴的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();
}
}
示例9: testSendAndReceiveOnAutoCreatedTopic
import javax.jms.TopicSession; //導入方法依賴的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();
}
}
示例10: testSendWithMultipleReceiversOnTopic
import javax.jms.TopicSession; //導入方法依賴的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();
}
}
示例11: main
import javax.jms.TopicSession; //導入方法依賴的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();
}
}
}
示例12: testPositiveJMSSelectorConsumerProducer
import javax.jms.TopicSession; //導入方法依賴的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();
}
示例13: testNegativeJMSSelectorConsumerProducer
import javax.jms.TopicSession; //導入方法依賴的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();
}
示例14: broadcastTextMessage
import javax.jms.TopicSession; //導入方法依賴的package包/類
/**
* Sends the passed in text message to a local topic
*
* @param topicName
* @param pMessage
* @param delaySeconds
* @throws ServiceLocatorException
*/
public void broadcastTextMessage(String topicName, String textMessage, int delaySeconds)
throws NamingException, JMSException, ServiceLocatorException {
if (mdwMessageProducer != null) {
mdwMessageProducer.broadcastMessageToTopic(topicName, textMessage);
}
else {
TopicConnectionFactory tFactory = null;
TopicConnection tConnection = null;
TopicSession tSession = null;
TopicPublisher tPublisher = null;
try {
// if (logger.isDebugEnabled()) logger.debug("broadcast JMS
// message: " +
// textMessage);
// cannot log above - causing recursive broadcasting
tFactory = getTopicConnectionFactory(null);
tConnection = tFactory.createTopicConnection();
tSession = tConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = getTopic(topicName);
tPublisher = tSession.createPublisher(topic);
// TODO: platform-independent delay
// WLMessageProducer wlMessageProducer =
// (WLMessageProducer)tPublisher;
// long delayInMilliSec = 0;
// if(pMinDelay > 0){
// delayInMilliSec = delaySeconds*1000;
// }
// wlMessageProducer.setTimeToDeliver(delayInMilliSec);
TextMessage message = tSession.createTextMessage();
tConnection.start();
message.setText(textMessage);
tPublisher.publish(message, DeliveryMode.PERSISTENT,
TextMessage.DEFAULT_DELIVERY_MODE, TextMessage.DEFAULT_TIME_TO_LIVE);
// }catch(ServiceLocatorException ex){
// ex.printStackTrace();
// never log exception here!!! infinite loop when publishing log
// messages
// throw new JMSException(ex.getMessage());
}
finally {
closeResources(tConnection, tSession, tPublisher);
}
}
}
示例15: testTopicNoLocalDurable
import javax.jms.TopicSession; //導入方法依賴的package包/類
@Test
public void testTopicNoLocalDurable() throws Exception {
connection.setClientID("forNoLocal-1");
connection.start();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("creating queue: " + topicName);
Topic dest = new ActiveMQTopic(topicName);
MessageConsumer nolocalConsumer = session.createDurableSubscriber(dest, "nolocal-subscriber1", "", true);
MessageConsumer consumer = session.createDurableSubscriber(dest, "normal-subscriber", null, false);
MessageConsumer selectorConsumer = session.createDurableSubscriber(dest, "selector-subscriber", "TESTKEY = 'test'", false);
MessageProducer producer = session.createProducer(dest);
final String body1 = "MfromAMQ-1";
final String body2 = "MfromAMQ-2";
TextMessage msg = session.createTextMessage(body1);
producer.send(msg);
msg = session.createTextMessage(body2);
msg.setStringProperty("TESTKEY", "test");
producer.send(msg);
//receive nolocal
TextMessage receivedMsg = (TextMessage) nolocalConsumer.receive(1000);
assertNull("nolocal consumer got: " + receivedMsg, receivedMsg);
//receive normal consumer
receivedMsg = (TextMessage) consumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body1, receivedMsg.getText());
receivedMsg = (TextMessage) consumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body2, receivedMsg.getText());
assertNull(consumer.receiveNoWait());
//selector should only receive one
receivedMsg = (TextMessage) selectorConsumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body2, receivedMsg.getText());
assertEquals("test", receivedMsg.getStringProperty("TESTKEY"));
assertNull(selectorConsumer.receiveNoWait());
//send from another connection
Connection anotherConn = this.factory.createConnection();
try {
anotherConn.start();
Session anotherSession = anotherConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer anotherProducer = anotherSession.createProducer(dest);
TextMessage anotherMsg = anotherSession.createTextMessage(body1);
anotherProducer.send(anotherMsg);
assertNotNull(consumer.receive(1000));
assertNull(selectorConsumer.receive(1000));
assertNotNull(nolocalConsumer.receive(1000));
} finally {
anotherConn.close();
}
session.close();
}