本文整理匯總了Java中javax.jms.Session.createTopic方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.createTopic方法的具體用法?Java Session.createTopic怎麽用?Java Session.createTopic使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.Session
的用法示例。
在下文中一共展示了Session.createTopic方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: putTopic
import javax.jms.Session; //導入方法依賴的package包/類
private void putTopic(List<String> events) throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME,
PASSWORD, BROKER_BIND_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic(DESTINATION_NAME);
MessageProducer producer = session.createProducer(destination);
for (String event : events) {
TextMessage message = session.createTextMessage();
message.setText(event);
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
示例2: createTopic
import javax.jms.Session; //導入方法依賴的package包/類
/**
* Create a topic
*
* @param topicName the topic name
*/
@PublicAtsApi
public void createTopic(
final String topicName ) {
try {
final Session session = loadSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
session.createConsumer(topic).close();
} catch (JMSException e) {
throw new JmsMessageException("Could not start listening for messages on topic " + topicName,
e);
} finally {
releaseSession(false);
}
}
示例3: createTopicConsumer
import javax.jms.Session; //導入方法依賴的package包/類
public MessageConsumer createTopicConsumer(String selector) throws JMSException {
if (isQueue) {
throw new IllegalArgumentException("Only for topic, not queue");
}
String consumerId = "consumer-" + UUID.randomUUID();
topicConnection = startConnection(consumerId);
Session session = topicConnection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(destinationName);
if (isDurable) {
if (selector != null) {
return session.createDurableSubscriber(topic, consumerId, selector, true);
} else {
return session.createDurableSubscriber(topic, consumerId);
}
} else {
if (selector != null) {
return session.createConsumer(topic, selector);
} else {
return session.createConsumer(topic);
}
}
}
示例4: testCreateTopic
import javax.jms.Session; //導入方法依賴的package包/類
@Test(timeout = 60000)
public void testCreateTopic() throws Exception {
JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(getTestName());
assertNotNull(topic);
assertEquals(getTestName(), topic.getTopicName());
assertTrue(topic instanceof MockJMSTopic);
}
示例5: testRepeatedCreateSessionProducerResultsInSame
import javax.jms.Session; //導入方法依賴的package包/類
@Test(timeout = 60000)
public void testRepeatedCreateSessionProducerResultsInSame() throws Exception {
JmsPoolConnection connection = (JmsPoolConnection) pooledFactory.createConnection();
assertTrue(pooledFactory.isUseAnonymousProducers());
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("test-topic");
JmsPoolMessageProducer producer = (JmsPoolMessageProducer) session.createProducer(destination);
MessageProducer original = producer.getMessageProducer();
assertNotNull(original);
session.close();
assertEquals(1, brokerService.getAdminView().getDynamicDestinationProducers().length);
for (int i = 0; i < 20; ++i) {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = (JmsPoolMessageProducer) session.createProducer(destination);
assertSame(original, producer.getMessageProducer());
session.close();
}
assertEquals(1, brokerService.getAdminView().getDynamicDestinationProducers().length);
connection.close();
pooledFactory.clear();
}
示例6: startListeningToTopic
import javax.jms.Session; //導入方法依賴的package包/類
/**
* Start listening for messages on topic
*
* @param topicName the topic name
*/
@PublicAtsApi
public void startListeningToTopic(
final String topicName ) {
final TopicInfo topicInfo = getTopicInfo(topicName);
if (topicInfo.isListening()) {
throw new JmsMessageException("We are already listening for messages on topic " + topicName);
}
try {
final Session session = loadSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
topicInfo.topicConsumer = session.createConsumer(topic);
topicInfo.topicConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage( Message message ) {
topicInfo.addMessage(message);
}
});
} catch (JMSException e) {
throw new JmsMessageException("Could not start listening for messages on topic " + topicName,
e);
}
}
示例7: sendMessage
import javax.jms.Session; //導入方法依賴的package包/類
public void sendMessage(Message message) throws JMSException {
Connection connection = null;
try {
connection = startConnection(); //try to be smarter here and initiate start connection
Session session = null;
try {
session = connection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
Destination dest;
if (isQueue) {
dest = session.createQueue(destinationName);
} else {
dest = session.createTopic(destinationName);
}
MessageProducer producer = session.createProducer(dest);
try {
if (isPersistant) producer.setDeliveryMode(DeliveryMode.PERSISTENT);
if (timeToLive > 0) producer.setTimeToLive(timeToLive);
producer.send(message);
} finally {
if (producer != null) producer.close();
}
} finally {
if (session != null) session.close();
}
} finally {
safeCloseConnection(connection);
}
}
示例8: getHeartbeats
import javax.jms.Session; //導入方法依賴的package包/類
/**
*
* @param uri
* @param topicName
* @param clazz
* @param monitorTime
* @return
*/
public Map<String, T> getHeartbeats(final URI uri, final String topicName, final Class<T> clazz, final long monitorTime) throws Exception {
final Map<String, T> ret = new HashMap<String, T>(3);
Connection topicConnection = null;
try {
ConnectionFactory connectionFactory = (ConnectionFactory)service.createConnectionFactory(uri);
topicConnection = connectionFactory.createConnection();
topicConnection.start();
Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
final MessageConsumer consumer = session.createConsumer(topic);
MessageListener listener = new MessageListener() {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage t = (TextMessage) message;
final T bean = (T)service.unmarshal(t.getText(), clazz);
Method nameMethod = bean.getClass().getMethod("getName");
ret.put((String)nameMethod.invoke(bean), bean);
}
} catch (Exception e) {
logger.error("Updating changed bean from topic", e);
}
}
};
consumer.setMessageListener(listener);
Thread.sleep(monitorTime);
return ret;
} catch (Exception ne) {
logger.error("Cannot listen to topic changes because command server is not there", ne);
return null;
} finally {
topicConnection.close();
}
}
示例9: testGetDevicesUsingString
import javax.jms.Session; //導入方法依賴的package包/類
public void testGetDevicesUsingString() throws Exception {
final ResponseConfiguration responseConfiguration = new ResponseConfiguration(ResponseType.ONE, 1000, TimeUnit.MILLISECONDS);
final List<DeviceRequest> responses = new ArrayList<>(1);
final ISubscriber<IBeanListener<DeviceRequest>> receive = eservice.createSubscriber(uri, EventConstants.DEVICE_RESPONSE_TOPIC);
// Just listen to our id changing.
receive.addListener("726c5d29-72f8-42e3-ba0c-51d26378065e", new IBeanListener<DeviceRequest>() {
@Override
public void beanChangePerformed(BeanEvent<DeviceRequest> evt) {
responses.add(evt.getBean());
responseConfiguration.countDown();
}
});
// Manually send a string without the extra java things...
final String rawString = "{\"uniqueId\":\"726c5d29-72f8-42e3-ba0c-51d26378065e\",\"deviceType\":\"RUNNABLE\",\"configure\":false}";
MessageProducer producer = null;
Connection send = null;
Session session = null;
try {
QueueConnectionFactory connectionFactory = (QueueConnectionFactory)eservice.getEventConnectorService().createConnectionFactory(uri);
send = connectionFactory.createConnection();
session = send.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(EventConstants.DEVICE_REQUEST_TOPIC);
producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
// Send the request
producer.send(session.createTextMessage(rawString));
} finally {
try {
if (session!=null) session.close();
} catch (JMSException e) {
throw new EventException("Cannot close session!", e);
}
}
responseConfiguration.latch(null); // Wait or die trying
if (responses.isEmpty()) throw new Exception("There was no response identified!");
if (responses.get(0).getDevices().size()<1) throw new Exception("There were no devices found and at least the mandelbrot example should have been!");
}
示例10: receiveMessage
import javax.jms.Session; //導入方法依賴的package包/類
public Message receiveMessage(long timeout, String selector) throws JMSException {
Connection connection = null;
Message result = null;
try {
connection = startConnection(); //try to be smarter here and start stable connection
Session session = null;
try {
session = connection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
Destination dest;
if (isQueue) {
dest = session.createQueue(destinationName);
} else {
dest = session.createTopic(destinationName);
}
MessageConsumer consumer;
if (selector != null) {
consumer = session.createConsumer(dest, selector);
} else {
consumer = session.createConsumer(dest);
}
try {
result = consumer.receive(timeout);
} finally {
if (consumer != null) consumer.close();
}
} finally {
if (session != null) session.close();
}
} finally {
safeCloseConnection(connection);
}
return result;
}
示例11: sendTopicMessage
import javax.jms.Session; //導入方法依賴的package包/類
private void sendTopicMessage(ActiveMqChannelVo queue, Object arg, boolean useTx, MqServiceContext context) throws Throwable {
ActiveMqSource mqSource = (ActiveMqSource) MqContainer.getInstance().getMqSourceManager().getMqSource(queue.getMsKey());
Connection connection = null;
Session session = null;
Throwable tx = null;
boolean transacted = false;
int acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
if (useTx) {
transacted = true;
acknowledgeMode = Session.SESSION_TRANSACTED;
}
try {
session = mqSource.getSession(transacted, acknowledgeMode);
if (null == session) {
connection = mqSource.getConnection();
session = connection.createSession(transacted, acknowledgeMode);
}
if (useTx) {
context.addTransactionObject(new ActiveMqTransactionObject(mqSource, session));
}
Destination destination = session.createTopic(queue.getName());
MessageProducer messageProducer = session.createProducer(destination);
TextMessage message = session.createTextMessage(((XCO) arg).toXMLString());
// int deliveryMode = (Integer) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_P_DELIVERYMODE);
// long timeToLive = (Long) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_P_TIMETOLIVE);
int deliveryMode = queue.getDeliveryMode();
long timeToLive = queue.getTimeToLive();
messageProducer.send(message, deliveryMode, Message.DEFAULT_PRIORITY, timeToLive);
log.info("send message to topic[" + queue.getName() + "]: " + ((XCO) arg).toXMLString());
} catch (Throwable e) {
tx = e;
} finally {
if (null != session && !useTx) {
mqSource.closeSession(session, transacted);
}
if (null != tx) {
throw tx;
}
}
}