本文整理汇总了Java中javax.jms.ConnectionFactory.createConnection方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectionFactory.createConnection方法的具体用法?Java ConnectionFactory.createConnection怎么用?Java ConnectionFactory.createConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.createConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putTopic
import javax.jms.ConnectionFactory; //导入方法依赖的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: receiveAndRespondWithMessageIdAsCorrelationId
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
示例3: testSetClientIDAfterConnectionStart
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
LOG.debug("running testRepeatedSetClientIDCalls()");
ConnectionFactory cf = createPooledConnectionFactory();
Connection conn = cf.createConnection();
// test: try to call setClientID() after start()
// should result in an exception
try {
conn.start();
conn.setClientID("newID3");
fail("Calling setClientID() after start() mut raise a JMSException.");
} catch (IllegalStateException ise) {
LOG.debug("Correctly received " + ise);
} finally {
conn.close();
((JmsPoolConnectionFactory) cf).stop();
}
LOG.debug("Test finished.");
}
示例4: sendMessages
import javax.jms.ConnectionFactory; //导入方法依赖的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: sendWithReplyToTemp
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
示例6: main
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
}
}
示例7: putQueue
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
private void putQueue(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.createQueue(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();
}
示例8: createConnection
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
/**
* @param brokerUrl
* The JMS broker url
* @param userId
* The user id
* @param password
* The password
* @return {@link Connection}
* @throws JMSException
* @throws NamingException
*/
private static Connection createConnection(String brokerUrl, String userId, String password)
throws JMSException, NamingException {
Context context = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) context.lookup(brokerUrl);
// Get a new instance of ConnectionFactory
// create a connection - providing the user id and password
Connection connection = factory.createConnection(userId, password);
return connection;
}
示例9: testConsumerProducerWithSsl
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
示例10: testConsumerProducerWithAutoAck
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
示例11: testSimpleSubmission
import javax.jms.ConnectionFactory; //导入方法依赖的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();
}
}
示例12: testCanExhaustSessions
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testCanExhaustSessions() throws Exception {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionUri);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE);
MessageConsumer consumer = session.createConsumer(destination);
for (int i = 0; i < NUM_MESSAGES; ++i) {
Message msg = consumer.receive(5000);
if (msg == null) {
return;
}
numReceived++;
if (numReceived % 20 == 0) {
LOG.debug("received " + numReceived + " messages ");
System.runFinalization();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
ExecutorService threads = Executors.newFixedThreadPool(2);
final CyclicBarrier barrier = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
LOG.trace("Starting threads to send messages!");
}
});
threads.execute(new TestRunner(barrier));
threads.execute(new TestRunner(barrier));
thread.join();
// we should expect that one of the threads will die because it cannot acquire a session,
// will throw an exception
assertEquals(NUM_MESSAGES, numReceived);
assertEquals(exceptionList.size(), 1);
}
示例13: testCanExhaustSessions
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testCanExhaustSessions() throws Exception {
final int totalMessagesExpected = NUM_MESSAGES * 2;
final CountDownLatch latch = new CountDownLatch(2);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionUri);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE);
MessageConsumer consumer = session.createConsumer(destination);
for (int i = 0; i < totalMessagesExpected; ++i) {
Message msg = consumer.receive(5000);
if (msg == null) {
return;
}
numReceived++;
if (numReceived % 20 == 0) {
LOG.debug("received " + numReceived + " messages ");
System.runFinalization();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
ExecutorService threads = Executors.newFixedThreadPool(2);
final CyclicBarrier barrier = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
LOG.trace("Starting threads to send messages!");
}
});
threads.execute(new TestRunner(barrier, latch));
threads.execute(new TestRunner(barrier, latch));
latch.await(2, TimeUnit.SECONDS);
thread.join();
assertEquals(totalMessagesExpected, numReceived);
}
示例14: testDlcWithBasicRecover
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
@Parameters({ "broker-hostname", "broker-port", "admin-username", "admin-password" })
@Test
public void testDlcWithBasicRecover(String brokerHostname,
String port,
String adminUsername,
String adminPassword) throws Exception {
String queueName = "testDlcWithBasicRecover";
String dlcQueueName = "amq.dlq";
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
.withQueue(queueName)
.withQueue(dlcQueueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
// publish message
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
producer.send(producerSession.createTextMessage("Message for DLC test"));
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
for (int iteration = 0; iteration < 6; iteration++) {
Message message = consumer.receive(5000);
Assert.assertNotNull(message, "Message was not received");
subscriberSession.recover();
}
Connection dlcConsumerConnection = connectionFactory.createConnection();
dlcConsumerConnection.start();
Session dlcConsumerSession = dlcConsumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer dlcConsumer = dlcConsumerSession.createConsumer((Destination) initialContextForQueue.lookup(
dlcQueueName));
Message dlcMessage = dlcConsumer.receive(5000);
Assert.assertNotNull(dlcMessage, "Dead lettered message was not received" + dlcMessage);
String originQueue = dlcMessage.getStringProperty("x-origin-queue");
Assert.assertEquals(originQueue, queueName, "Origin queue name did not match" + dlcMessage);
String originExchange = dlcMessage.getStringProperty("x-origin-exchange");
Assert.assertEquals(originExchange, "amq.direct", "Origin exchange name did not match" + dlcMessage);
String originRoutingKey = dlcMessage.getStringProperty("x-origin-routing-key");
Assert.assertEquals(originRoutingKey, queueName, "Origin routing key did not match" + dlcMessage);
dlcConsumerConnection.close();
connection.close();
}
示例15: testConsumerWithBasicReject
import javax.jms.ConnectionFactory; //导入方法依赖的package包/类
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" })
@Test
public void testConsumerWithBasicReject(String port,
String adminUsername,
String adminPassword,
String brokerHostname) throws Exception {
System.setProperty("AndesAckWaitTimeOut", "5000");
String queueName = "testConsumerWithBasicReject";
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
.withQueue(queueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
// publish message
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
producer.send(producerSession.createTextMessage("Test message for reject test"));
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
Message message = consumer.receive(5000);
Assert.assertNotNull(message, "Message was not received");
message = consumer.receive(10000);
Assert.assertNotNull(message, "Requeued Message was not received");
Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was not set");
message.acknowledge();
connection.close();
}