本文整理匯總了Java中javax.jms.Session類的典型用法代碼示例。如果您正苦於以下問題:Java Session類的具體用法?Java Session怎麽用?Java Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Session類屬於javax.jms包,在下文中一共展示了Session類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateMessages
import javax.jms.Session; //導入依賴的package包/類
/**
* Generates JMS messages
*/
@PostConstruct
public void generateMessages() throws JMSException {
for (int i = 0; i < messageCount; i++) {
final int index = i;
final String text = "Message number is " + i + ".";
template.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(text);
message.setIntProperty(MESSAGE_COUNT, index);
System.out.println("Sending message: " + text);
return message;
}
});
}
}
示例2: send
import javax.jms.Session; //導入依賴的package包/類
public static void send(String queueName, String text, int delayMillis) {
EXECUTOR.submit(() -> {
try {
logger.info("*** artificial delay {}: {}", queueName, delayMillis);
Thread.sleep(delayMillis);
Connection connection = getConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queueName);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(text);
producer.send(message);
logger.info("*** sent message {}: {}", queueName, text);
session.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
示例3: createMessageProducer
import javax.jms.Session; //導入依賴的package包/類
public static MessageProducer createMessageProducer(
Session session,
Destination destination,
MessageProducerOption producerOption) throws JMSException {
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryDelay(producerOption.getDeliveryDelay());
producer.setDeliveryMode(producerOption.getDeliveryMode());
producer.setDisableMessageTimestamp(producerOption.isDisableMessageTimestamp());
producer.setDisableMessageID(producerOption.isDisableMessageId());
producer.setPriority(producerOption.getPriority());
producer.setTimeToLive(producerOption.getTimeToLive());
return producer;
}
示例4: plaatsAfnemerberichten
import javax.jms.Session; //導入依賴的package包/類
@Override
public void plaatsAfnemerberichten(final List<AfnemerBericht> afnemerBerichten) {
try {
afnemersJmsTemplate.execute((final Session session, final MessageProducer producer) -> {
for (final AfnemerBericht afnemerBericht : afnemerBerichten) {
final SynchronisatieBerichtGegevens synchronisatieBerichtGegevens = afnemerBericht.getSynchronisatieBerichtGegevens();
final Message message = session.createTextMessage(serializer.serialiseerNaarString(synchronisatieBerichtGegevens));
message.setStringProperty(LeveringConstanten.JMS_MESSAGEGROUP_HEADER,
String.valueOf(synchronisatieBerichtGegevens.getArchiveringOpdracht().getOntvangendePartijId()));
final ToegangLeveringsAutorisatie toegang = afnemerBericht.getToegangLeveringsAutorisatie();
LOGGER.info("Zet bericht op de queue voor afnemer {} en kanaal {}",
toegang.getGeautoriseerde().getPartij().getNaam(),
toegang.getLeveringsautorisatie().getStelsel());
producer.send(message);
}
return null;
});
} catch (final JmsException e) {
LOGGER.error("fout in verzenden berichten naar afnemer queue", e);
throw new BrpServiceRuntimeException(e);
}
}
示例5: sendObjectMsgSingleSession
import javax.jms.Session; //導入依賴的package包/類
private void sendObjectMsgSingleSession(List<? extends Serializable> objectsToSend) throws JMSException {
Session session = null;
Connection conn = null;
try {
conn = qFactory.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
for (Serializable objectToSend : objectsToSend) {
ObjectMessage msg = session.createObjectMessage();
msg.setObject(objectToSend);
producer.send(msg);
}
} finally {
closeSession(session);
closeConnection(conn);
}
}
示例6: send
import javax.jms.Session; //導入依賴的package包/類
/**
* Sends the given {@code events} to the configured JMS Topic. It takes the current Unit of Work
* into account when available. Otherwise, it simply publishes directly.
*
* @param events the events to publish on the JMS Message Broker
*/
protected void send(List<? extends EventMessage<?>> events) {
try (TopicConnection topicConnection = connectionFactory.createTopicConnection()) {
int ackMode = isTransacted ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE;
TopicSession topicSession = topicConnection.createTopicSession(isTransacted, ackMode);
try (TopicPublisher publisher = topicSession.createPublisher(topic)) {
for (EventMessage event : events) {
Message jmsMessage = messageConverter.createJmsMessage(event, topicSession);
doSendMessage(publisher, jmsMessage);
}
} finally {
handleTransaction(topicSession);
}
} catch (JMSException ex) {
throw new EventPublicationFailedException(
"Unable to establish TopicConnection to JMS message broker.", ex);
}
}
示例7: testGetTopicSubscriber
import javax.jms.Session; //導入依賴的package包/類
@Test
public void testGetTopicSubscriber() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createTemporaryQueue();
JmsPoolQueueReceiver receiver = (JmsPoolQueueReceiver) session.createReceiver(queue);
assertNotNull(receiver.getQueueReceiver());
assertTrue(receiver.getQueueReceiver() instanceof MockJMSQueueReceiver);
receiver.close();
try {
receiver.getQueueReceiver();
fail("Cannot read state on closed receiver");
} catch (IllegalStateException ise) {}
}
示例8: testReceiveTimed
import javax.jms.Session; //導入依賴的package包/類
@Test
public void testReceiveTimed() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
Queue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue, "Color = Red");
assertNull(consumer.receive(1));
consumer.close();
try {
consumer.receive(1);
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateException ise) {}
}
示例9: doTestCreateMessageProducer
import javax.jms.Session; //導入依賴的package包/類
private void doTestCreateMessageProducer(boolean useAnonymousProducers) throws JMSException {
cf.setUseAnonymousProducers(useAnonymousProducers);
JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue1 = session.createTemporaryQueue();
Queue queue2 = session.createTemporaryQueue();
JmsPoolMessageProducer producer1 = (JmsPoolMessageProducer) session.createProducer(queue1);
JmsPoolMessageProducer producer2 = (JmsPoolMessageProducer) session.createProducer(queue2);
if (useAnonymousProducers) {
assertSame(producer1.getMessageProducer(), producer2.getMessageProducer());
} else {
assertNotSame(producer1.getMessageProducer(), producer2.getMessageProducer());
}
connection.close();
}
示例10: doTestCreateTopicPublisher
import javax.jms.Session; //導入依賴的package包/類
private void doTestCreateTopicPublisher(boolean useAnonymousProducers) throws JMSException {
cf.setUseAnonymousProducers(useAnonymousProducers);
JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic1 = session.createTopic("Topic-1");
Topic topic2 = session.createTopic("Topic-2");
JmsPoolTopicPublisher publisher1 = (JmsPoolTopicPublisher) session.createPublisher(topic1);
JmsPoolTopicPublisher publisher2 = (JmsPoolTopicPublisher) session.createPublisher(topic2);
if (useAnonymousProducers) {
assertSame(publisher1.getMessageProducer(), publisher2.getMessageProducer());
} else {
assertNotSame(publisher1.getMessageProducer(), publisher2.getMessageProducer());
}
connection.close();
}
示例11: doTestCreateQueueSender
import javax.jms.Session; //導入依賴的package包/類
private void doTestCreateQueueSender(boolean useAnonymousProducers) throws JMSException {
cf.setUseAnonymousProducers(useAnonymousProducers);
JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue1 = session.createTemporaryQueue();
Queue queue2 = session.createTemporaryQueue();
JmsPoolQueueSender sender1 = (JmsPoolQueueSender) session.createSender(queue1);
JmsPoolQueueSender sender2 = (JmsPoolQueueSender) session.createSender(queue2);
if (useAnonymousProducers) {
assertSame(sender1.getMessageProducer(), sender2.getMessageProducer());
} else {
assertNotSame(sender1.getMessageProducer(), sender2.getMessageProducer());
}
connection.close();
}
示例12: sendAndReceive
import javax.jms.Session; //導入依賴的package包/類
@Test
public void sendAndReceive() throws Exception {
jmsTemplate.send("TEST.FOO", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello world");
}
});
TextMessage received = (TextMessage) jmsTemplate.receive("TEST.FOO");
assertEquals("Hello world", received.getText());
jmsTemplate.convertAndSend("TEST.FOO", "Hello world");
assertEquals("Hello world", jmsTemplate.receiveAndConvert("TEST.FOO"));
List<MockSpan> mockSpans = mockTracer.finishedSpans();
assertEquals(4, mockSpans.size());
checkSpans(mockSpans);
assertNull(mockTracer.activeSpan());
}
示例13: validateFactoryCreationWithActiveMQLibraries
import javax.jms.Session; //導入依賴的package包/類
/**
* This test simply validates that {@link ConnectionFactory} can be setup by
* pointing to the location of the client libraries at runtime. It uses
* ActiveMQ which is not present at the POM but instead pulled from Maven
* repo using {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which
* implies that for this test to run the computer must be connected to the
* Internet. If computer is not connected to the Internet, this test will
* quietly fail logging a message.
*/
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
try {
String libPath = TestUtils.setupActiveMqLibForTesting(true);
TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
runner.addControllerService("cfProvider", cfProvider);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI,
"vm://localhost?broker.persistent=false");
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
"org.apache.activemq.ActiveMQConnectionFactory");
runner.enableControllerService(cfProvider);
runner.assertValid(cfProvider);
Connection connection = cfProvider.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("myqueue");
MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello");
producer.send(message);
assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
connection.stop();
connection.close();
} catch (Exception e) {
logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
}
}
示例14: getSession
import javax.jms.Session; //導入依賴的package包/類
protected Session getSession() {
if (session == null) {
synchronized (this) {
if (closed)
throw new IllegalStateRuntimeException("Context is closed");
if (session == null) {
try {
session = connection.createSession(sessionMode);
} catch (JMSException e) {
throw Utils.convertToRuntimeException(e);
}
}
}
}
return session;
}
示例15: sendMessage
import javax.jms.Session; //導入依賴的package包/類
public void sendMessage() {
jmsTemplate.send(notifyTopic, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
int delay=10*1000;
System.out.println("生產消消息");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
message.setStringProperty("objectType", "user");
return message;
}
});
}