下面列出了javax.jms.Connection#getClientID ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void start() throws JMSException {
String selector = "next = '" + myId + "'";
try {
ConnectionFactory factory = template.getConnectionFactory();
final Connection c = connection = factory.createConnection();
// we might be a reusable connection in spring
// so lets only set the client ID once if its not set
synchronized (c) {
if (c.getClientID() == null) {
c.setClientID(myId);
}
}
connection.start();
session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
consumer = session.createConsumer(destination, selector, false);
consumer.setMessageListener(this);
} catch (JMSException ex) {
LOG.error("", ex);
throw ex;
}
}
/**
* user/pwd with preconfigured clientID, should return preconf
*/
@Test
public void testPreConfClientID() throws Exception {
Connection conn = null;
try {
ActiveMQServerTestCase.deployConnectionFactory("dilbert-id", "preConfcf", "preConfcf");
ConnectionFactory cf = (ConnectionFactory) getInitialContext().lookup("preConfcf");
conn = cf.createConnection("guest", "guest");
String clientID = conn.getClientID();
ProxyAssertSupport.assertEquals("Invalid ClientID", "dilbert-id", clientID);
} finally {
if (conn != null) {
conn.close();
}
ActiveMQServerTestCase.undeployConnectionFactory("preConfcf");
}
}
/**
* Test cached connection when the transport.jms.CacheLevel is 1
*
* @throws Exception
*/
@Test
public void testCacheLevelOne() throws Exception {
String queueName = "testCaching1";
Properties jmsProperties = JMSTestsUtils.getJMSPropertiesForDestination(queueName, PROVIDER_URL, true);
JMSBrokerController brokerController = new JMSBrokerController(PROVIDER_URL, jmsProperties);
jmsProperties.put(JMSConstants.PARAM_CACHE_LEVEL, "1");
try {
brokerController.startProcess();
Queue queue = brokerController.connect(queueName, true);
CachedJMSConnectionFactory cachedJMSConnectionFactory = new CachedJMSConnectionFactory(jmsProperties);
Connection connection1 = cachedJMSConnectionFactory.getConnection(null, null);
String clientID1 = connection1.getClientID();
Session session1 = cachedJMSConnectionFactory.getSession(connection1);
MessageConsumer consumer1 = cachedJMSConnectionFactory.getMessageConsumer(session1, queue);
Connection connection2 = cachedJMSConnectionFactory.getConnection(null, null);
Session session2 = cachedJMSConnectionFactory.getSession(connection2);
MessageConsumer consumer2 = cachedJMSConnectionFactory.getMessageConsumer(session2, queue);
Assert.assertEquals("Connection should be cached", clientID1, connection2.getClientID());
Assert.assertNotSame("Session should not be cached", session1, session2);
Assert.assertNotSame("Message Consumer should not be cached", ((ActiveMQMessageConsumer) consumer1).
getConsumerId().toString(), ((ActiveMQMessageConsumer) consumer2).getConsumerId().toString());
cachedJMSConnectionFactory.closeConnection();
Connection connection3 = cachedJMSConnectionFactory.getConnection(null, null);
Assert.assertNotSame("Connection should be closed", clientID1, connection3.getClientID());
} finally {
brokerController.disconnect();
brokerController.stopProcess();
}
}
@Test
public void testGetClientID() throws Exception {
Connection connection = createConnection();
String clientID = connection.getClientID();
// We don't currently set client ids on the server, so this should be null.
// In the future we may provide connection factories that set a specific client id
// so this may change
ProxyAssertSupport.assertNull(clientID);
connection.close();
}
/**
* Try setting client ID
*/
@Test
public void testSetClientID() throws Exception {
Connection conn = createConnection();
conn.setClientID("myID");
String clientID = conn.getClientID();
ProxyAssertSupport.assertEquals("Invalid ClientID", "myID", clientID);
}