本文整理汇总了Java中javax.jms.Connection类的典型用法代码示例。如果您正苦于以下问题:Java Connection类的具体用法?Java Connection怎么用?Java Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Connection类属于javax.jms包,在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putTopic
import javax.jms.Connection; //导入依赖的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.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();
}
示例3: createMessage
import javax.jms.Connection; //导入依赖的package包/类
public Message createMessage(Object messageObject) throws JMSException {
Connection connection = null;
Message result = null;
try {
connection = startConnection();
Session session = null;
try {
session = connection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
if (messageObject == null) {
result = session.createMessage();
} else {
if (messageObject instanceof String) {
result = session.createTextMessage((String) messageObject);
} else {
result = session.createObjectMessage((Serializable) messageObject);
}
}
} finally {
if (session != null) session.close();
}
} finally {
safeCloseConnection(connection);
}
return result;
}
示例4: sendObjectMsgSingleSession
import javax.jms.Connection; //导入依赖的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);
}
}
示例5: testCreateSession
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testCreateSession() throws Exception {
Connection connection = cf.createConnection();
Session session1 = connection.createSession();
Session session2 = connection.createSession();
assertNotSame(session1, session2);
assertEquals(session1.getAcknowledgeMode(), Session.AUTO_ACKNOWLEDGE);
assertEquals(session2.getAcknowledgeMode(), Session.AUTO_ACKNOWLEDGE);
JmsPoolSession wrapperSession1 = (JmsPoolSession) session1;
JmsPoolSession wrapperSession2 = (JmsPoolSession) session2;
assertNotSame(wrapperSession1.getSession(), wrapperSession2.getSession());
}
示例6: testSetClientIDTwiceWithSameID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
Connection connection = cf.createConnection();
// test: call setClientID("newID") twice
// this should be tolerated and not result in an exception
connection.setClientID("newID");
try {
connection.setClientID("newID");
connection.start();
connection.close();
} catch (IllegalStateException ise) {
LOG.error("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
fail("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
} finally {
cf.stop();
}
LOG.debug("Test finished.");
}
示例7: testSetClientIDTwiceWithDifferentID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithDifferentID() throws Exception {
Connection connection = cf.createConnection();
// test: call setClientID() twice with different IDs
// this should result in an IllegalStateException
connection.setClientID("newID1");
try {
connection.setClientID("newID2");
fail("calling Connection.setClientID() twice with different clientID must raise an IllegalStateException");
} catch (IllegalStateException ise) {
LOG.debug("Correctly received " + ise);
} finally {
connection.close();
cf.stop();
}
LOG.debug("Test finished.");
}
示例8: testSetClientIDAfterConnectionStart
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
Connection connection = cf.createConnection();
// test: try to call setClientID() after start()
// should result in an exception
try {
connection.start();
connection.setClientID("newID3");
fail("Calling setClientID() after start() mut raise a JMSException.");
} catch (IllegalStateException ise) {
LOG.debug("Correctly received " + ise);
} finally {
connection.close();
cf.stop();
}
LOG.debug("Test finished.");
}
示例9: testConnectionsAreRotated
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testConnectionsAreRotated() throws Exception {
JmsPoolConnectionFactory cf = createPooledConnectionFactory();
cf.setMaxConnections(10);
Connection previous = null;
// Front load the pool.
for (int i = 0; i < 10; ++i) {
cf.createConnection();
}
for (int i = 0; i < 100; ++i) {
Connection current = ((JmsPoolConnection) cf.createConnection()).getConnection();
assertNotSame(previous, current);
previous = current;
}
cf.stop();
}
示例10: testSetClientIDTwiceWithSameID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
LOG.debug("running testRepeatedSetClientIDCalls()");
// test: call setClientID("newID") twice
// this should be tolerated and not result in an exception
ConnectionFactory cf = createPooledConnectionFactory();
Connection conn = cf.createConnection();
conn.setClientID("newID");
try {
conn.setClientID("newID");
conn.start();
conn.close();
} catch (IllegalStateException ise) {
LOG.error("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
fail("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
} finally {
((JmsPoolConnectionFactory) cf).stop();
}
LOG.debug("Test finished.");
}
示例11: testSetClientIDTwiceWithDifferentID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithDifferentID() throws Exception {
LOG.debug("running testRepeatedSetClientIDCalls()");
ConnectionFactory cf = createPooledConnectionFactory();
Connection conn = cf.createConnection();
// test: call setClientID() twice with different IDs
// this should result in an IllegalStateException
conn.setClientID("newID1");
try {
conn.setClientID("newID2");
fail("calling Connection.setClientID() twice with different clientID must raise an IllegalStateException");
} catch (IllegalStateException ise) {
LOG.debug("Correctly received " + ise);
} finally {
conn.close();
((JmsPoolConnectionFactory) cf).stop();
}
LOG.debug("Test finished.");
}
示例12: testSetClientIDAfterConnectionStart
import javax.jms.Connection; //导入依赖的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.");
}
示例13: testConnectionsAreRotated
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testConnectionsAreRotated() throws Exception {
cf.setMaxConnections(10);
Connection previous = null;
// Front load the pool.
for (int i = 0; i < 10; ++i) {
cf.createConnection();
}
for (int i = 0; i < 100; ++i) {
Connection current = ((JmsPoolConnection) cf.createConnection()).getConnection();
assertNotSame(previous, current);
previous = current;
}
cf.stop();
}
示例14: testSetClientIDTwiceWithSameID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
// test: call setClientID("newID") twice
// this should be tolerated and not result in an exception
Connection conn = cf.createConnection();
conn.setClientID("newID");
try {
conn.setClientID("newID");
conn.start();
conn.close();
} catch (IllegalStateException ise) {
LOG.error("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
fail("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
} finally {
cf.stop();
}
LOG.debug("Test finished.");
}
示例15: testSetClientIDTwiceWithDifferentID
import javax.jms.Connection; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDTwiceWithDifferentID() throws Exception {
Connection conn = cf.createConnection();
// test: call setClientID() twice with different IDs
// this should result in an IllegalStateException
conn.setClientID("newID1");
try {
conn.setClientID("newID2");
fail("calling Connection.setClientID() twice with different clientID must raise an IllegalStateException");
} catch (IllegalStateException ise) {
LOG.debug("Correctly received " + ise);
} finally {
conn.close();
cf.stop();
}
LOG.debug("Test finished.");
}