本文整理汇总了Java中javax.jms.Connection.createSession方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.createSession方法的具体用法?Java Connection.createSession怎么用?Java Connection.createSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.Connection
的用法示例。
在下文中一共展示了Connection.createSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: receiveAndRespondWithMessageIdAsCorrelationId
import javax.jms.Connection; //导入方法依赖的package包/类
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
final javax.jms.Message inMessage = consumer.receive();
String requestMessageId = inMessage.getJMSMessageID();
LOG.debug("Received message " + requestMessageId);
final TextMessage replyMessage = session.createTextMessage("Result");
replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
LOG.debug("Sending reply to " + inMessage.getJMSReplyTo());
producer.send(replyMessage);
producer.close();
consumer.close();
session.close();
connection.close();
}
示例2: main
import javax.jms.Connection; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Connection connection = null;
String csvData = System.getProperty(CSVDATA);
if(CSVDATA == null || CSVDATA.equals(""))
throw new RuntimeException("LoyaltyCardManager.main() must pass the "+CSVDATA +" system property With format OPERATION;USERID;FIRSTNAME;LASTNAME;TRXID;TRXFEESAMOUNT;CURRENCY");
System.out.println("LoyaltyCardManager() will connect to router: "+ROUTER_URL+" : at the following address: "+QUEUE_NAME);
ConnectionFactory connectionFactory = new JmsConnectionFactory(ROUTER_URL);
try {
// Step 1. Create an AMQP qpid connection
connection = connectionFactory.createConnection();
// Step 2. Create a JMS session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 3. Create a Producer
Queue fidelityRequestQueue = session.createQueue(QUEUE_NAME);
MessageProducer beosbankFidelityRequestProducer = session.createProducer(fidelityRequestQueue);
// Step 4. send a CSV Text Data on user transactions
beosbankFidelityRequestProducer.send(session.createTextMessage(csvData));
System.out.println("\nmessage sent:"+ csvData+" \n");
} finally {
if (connection != null) {
// Step 9. close the connection
connection.close();
}
}
}
示例3: sendWithReplyToTemp
import javax.jms.Connection; //导入方法依赖的package包/类
private void sendWithReplyToTemp(ConnectionFactory cf, String serviceQueue) throws JMSException, InterruptedException {
Connection connection = cf.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = session.createTemporaryQueue();
TextMessage msg = session.createTextMessage("Request");
msg.setJMSReplyTo(tempQueue);
MessageProducer producer = session.createProducer(session.createQueue(serviceQueue));
producer.send(msg);
MessageConsumer consumer = session.createConsumer(tempQueue);
Message replyMsg = consumer.receive();
assertNotNull(replyMsg);
LOG.debug("Reply message: {}", replyMsg);
consumer.close();
producer.close();
session.close();
connection.close();
}
示例4: sendMessages
import javax.jms.Connection; //导入方法依赖的package包/类
public void sendMessages(ConnectionFactory connectionFactory) throws Exception {
for (int i = 0; i < NUM_MESSAGES; i++) {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE);
MessageProducer producer = session.createProducer(destination);
String msgTo = "hello";
TextMessage message = session.createTextMessage(msgTo);
producer.send(message);
connection.close();
LOG.debug("sent " + i + " messages using " + connectionFactory.getClass());
}
}
示例5: testSimpleSubmission
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testSimpleSubmission() throws Exception {
StatusBean bean = doSubmit();
// Manually take the submission from the list not using event service for isolated test
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(submitter.getUri());
Connection connection = connectionFactory.createConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(IEventService.SUBMISSION_QUEUE);
final MessageConsumer consumer = session.createConsumer(queue);
connection.start();
TextMessage msg = (TextMessage)consumer.receive(1000);
IMarshallerService marshaller = new MarshallerService(new PointsModelMarshaller());
StatusBean fromQ = marshaller.unmarshal(msg.getText(), StatusBean.class);
if (!fromQ.equals(bean)) throw new Exception("The bean from the queue was not the same as that submitted! q="+fromQ+" submit="+bean);
} finally {
consumer.disconnect();
connection.close();
}
}
示例6: run
import javax.jms.Connection; //导入方法依赖的package包/类
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("HELLOWORLD.TESTQ");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
示例7: doTestCreateSessionWithGivenAckModeAndTXFlag
import javax.jms.Connection; //导入方法依赖的package包/类
public void doTestCreateSessionWithGivenAckModeAndTXFlag(boolean transacted, int ackMode) throws Exception {
Connection connection = cf.createConnection();
if (!transacted && ackMode == Session.SESSION_TRANSACTED) {
try {
connection.createSession(transacted, ackMode);
fail("Should not allow non-transacted session with SESSION_TRANSACTED");
} catch (JMSException jmsex) {}
} else {
Session session1 = connection.createSession(transacted, ackMode);
Session session2 = connection.createSession(transacted, ackMode);
assertNotSame(session1, session2);
if (transacted) {
assertEquals(session1.getAcknowledgeMode(), Session.SESSION_TRANSACTED);
assertEquals(session2.getAcknowledgeMode(), Session.SESSION_TRANSACTED);
} else {
assertEquals(session1.getAcknowledgeMode(), ackMode);
assertEquals(session2.getAcknowledgeMode(), ackMode);
}
JmsPoolSession wrapperSession1 = (JmsPoolSession) session1;
JmsPoolSession wrapperSession2 = (JmsPoolSession) session2;
assertNotSame(wrapperSession1.getSession(), wrapperSession2.getSession());
}
}
示例8: testFailedCreateConsumerConnectionStillWorks
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testFailedCreateConsumerConnectionStillWorks() throws JMSException {
// User can write but not read
user.setCanConsumeAll(false);
Connection connection = null;
try {
connection = cf.createConnection("admin", "admin");
} catch (JMSSecurityException jmsse) {
fail("Should not be able to create connection using bad credentials");
}
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("test");
try {
session.createConsumer(queue);
fail("Should fail to create consumer");
} catch (JMSSecurityException ex) {
LOG.debug("Caught expected security error");
}
MessageProducer producer = session.createProducer(queue);
producer.close();
connection.close();
}
示例9: testTempQueueIssue
import javax.jms.Connection; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testTempQueueIssue() throws JMSException, InterruptedException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
"vm://localhost?broker.persistent=false&broker.useJmx=false");
final JmsPoolConnectionFactory cf = new JmsPoolConnectionFactory();
cf.setConnectionFactory(factory);
Connection connection = cf.createConnection();
assertNotNull(connection);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
LOG.info("First connection was {}", connection);
// This order seems to matter to reproduce the issue
connection.close();
session.close();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
receiveAndRespondWithMessageIdAsCorrelationId(cf, SERVICE_QUEUE);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
sendWithReplyToTemp(cf, SERVICE_QUEUE);
cf.stop();
}
示例10: testTemporaryQueueWithMultipleConnectionUsers
import javax.jms.Connection; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testTemporaryQueueWithMultipleConnectionUsers() throws Exception {
Connection pooledConnection = null;
Connection pooledConnection2 = null;
Session session = null;
Session session2 = null;
Queue tempQueue = null;
Queue normalQueue = null;
pooledConnection = pooledFactory.createConnection();
session = pooledConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
tempQueue = session.createTemporaryQueue();
LOG.info("Created queue named: " + tempQueue.getQueueName());
assertEquals(1, countBrokerTemporaryQueues());
pooledConnection2 = pooledFactory.createConnection();
session2 = pooledConnection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
normalQueue = session2.createQueue("queue:FOO.TEST");
LOG.info("Created queue named: " + normalQueue.getQueueName());
// didn't create a temp queue on pooledConnection2 so we should still have a temp queue
pooledConnection2.close();
assertEquals(1, countBrokerTemporaryQueues());
// after closing pooledConnection, where we created the temp queue, there should
// be no temp queues left
pooledConnection.close();
assertEquals(0, countBrokerTemporaryQueues());
}
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:31,代码来源:PooledConnectionFactoryWithTemporaryDestinationsTest.java
示例11: testConsumerProducerWithSsl
import javax.jms.Connection; //导入方法依赖的package包/类
@Parameters({ "broker-ssl-port"})
@Test
public void testConsumerProducerWithSsl(String port) throws Exception {
String queueName = "testConsumerProducerWithAutoAck";
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder("admin", "admin", "localhost", port)
.enableSsl()
.withQueue(queueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
// publish 100 messages
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
int numberOfMessages = 100;
for (int i = 0; i < numberOfMessages; i++) {
producer.send(producerSession.createTextMessage("Test message " + i));
}
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
for (int i = 0; i < numberOfMessages; i++) {
Message message = consumer.receive(1000);
Assert.assertNotNull(message, "Message #" + i + " was not received");
}
connection.close();
}
示例12: testConsumerProducerWithAutoAck
import javax.jms.Connection; //导入方法依赖的package包/类
@Parameters({ "broker-port"})
@Test
public void testConsumerProducerWithAutoAck(String port) throws Exception {
String queueName = "testConsumerProducerWithAutoAck";
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder("admin", "admin", "localhost", port)
.withQueue(queueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
// publish 100 messages
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
int numberOfMessages = 100;
for (int i = 0; i < numberOfMessages; i++) {
producer.send(producerSession.createTextMessage("Test message " + i));
}
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
for (int i = 0; i < numberOfMessages; i++) {
Message message = consumer.receive(5000);
Assert.assertNotNull(message, "Message #" + i + " was not received");
}
connection.close();
}
示例13: start
import javax.jms.Connection; //导入方法依赖的package包/类
@Override
public void start() throws Throwable {
boolean durableSubscribers = false;// 持久化订阅
String clientID = null;
if (ChannelType.Topic == queue.getType()) {
// durableSubscribers = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_DURABLESUBSCRIBERS);
durableSubscribers = queue.isDurableSubscribers();
if (durableSubscribers) {
// clientID = (String) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_CLIENTID);
clientID = queue.getClientID();
if (null == clientID) {
throw new XMLParseException("durable subscribers is missing a clientID: " + queue.getName());
}
}
}
ActiveMqSource mqSource = (ActiveMqSource) MqContainer.getInstance().getMqSourceManager().getMqSource(queue.getMsKey());
Connection connection = null;
if (!durableSubscribers) {
connection = mqSource.getConnection();
} else {
connection = mqSource.getConnection(clientID);
this.durableSubscriberConn = connection;
}
// boolean transacted = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_TRANSACTED);
// int acknowledgeMode = (Integer) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_ACKNOWLEDGEMODE);
boolean transacted = queue.isTransacted();
int acknowledgeMode = queue.getAcknowledgeMode();
session = connection.createSession(transacted, acknowledgeMode);
Destination destination = null;
if (ChannelType.Queue == queue.getType()) {
destination = session.createQueue(queue.getName());
typeStr = "queue";
} else if (ChannelType.Topic == queue.getType()) {
destination = session.createTopic(queue.getName());
typeStr = "topic";
}
MessageConsumer messageConsumer = null;
if (!durableSubscribers) {
messageConsumer = session.createConsumer(destination);
} else {
messageConsumer = session.createDurableSubscriber((Topic) destination, clientID);
}
running = true;
// boolean asynReceiveMessages = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_ASYNRECEIVEMESSAGES);
boolean asynReceiveMessages = queue.isAsynReceive();
if (asynReceiveMessages) {
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
// TODO 如果是session.commit();, 是否需要使用同步关键字, 防止提交别的线程的东西
// System.out.println("####################:" + Thread.currentThread().getName());
processMessage(message);
} catch (Throwable e) {
log.error("listen to the [" + queue.getName() + "] error.", e);
}
}
});
} else {
// long receiveTimeout = (Long) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_RECEIVETIMEOUT);
long receiveTimeout = queue.getReceiveTimeout();
startSyncReceiveThread(messageConsumer, receiveTimeout);
}
}
示例14: testConnectionFailures
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testConnectionFailures() throws Exception {
final CountDownLatch failed = new CountDownLatch(1);
Connection connection = pooledConnFact.createConnection();
LOG.info("Fetched new connection from the pool: {}", connection);
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.info("Pooled Connection failed");
failed.countDown();
}
});
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(getTestName());
MessageProducer producer = session.createProducer(queue);
brokerService.stop();
assertTrue(failed.await(15, TimeUnit.SECONDS));
createBroker();
try {
producer.send(session.createMessage());
fail("Should be disconnected");
} catch (JMSException ex) {
LOG.info("Producer failed as expected: {}", ex.getMessage());
}
Connection connection2 = pooledConnFact.createConnection();
assertNotSame(connection, connection2);
LOG.info("Fetched new connection from the pool: {}", connection2);
session = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection2.close();
pooledConnFact.stop();
}
示例15: sendTopicMessage
import javax.jms.Connection; //导入方法依赖的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;
}
}
}