下面列出了怎么用javax.jms.QueueConnectionFactory的API类实例代码及写法,或者点击链接到github查看源代码。
private ConnectionFactory createConnectionFactory() {
if (this.connectionFactory != null) {
return this.connectionFactory;
}
if (ctx == null) {
return null;
}
try {
if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) {
this.connectionFactory = (QueueConnectionFactory) ctx.lookup(this.connectionFactoryString);
} else if (this.destinationType.equals(JMSConstants.JMSDestinationType.TOPIC)) {
this.connectionFactory = (TopicConnectionFactory) ctx.lookup(this.connectionFactoryString);
}
} catch (NamingException e) {
logger.error(
"Naming exception while obtaining connection factory for '" + this.connectionFactoryString + "'",
e);
}
return this.connectionFactory;
}
@Test
public void testWithQueueConnectionFactoryAndJms11Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
given(cf.createConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createConnection();
Connection con2 = scf.createConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
@Test
public void testWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
given(cf.createQueueConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createQueueConnection();
Connection con2 = scf.createQueueConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
/**
* This implementation delegates to the {@code createQueueConnection(username, password)}
* method of the target QueueConnectionFactory, passing in the specified user credentials.
* If the specified username is empty, it will simply delegate to the standard
* {@code createQueueConnection()} method of the target ConnectionFactory.
* @param username the username to use
* @param password the password to use
* @return the Connection
* @see javax.jms.QueueConnectionFactory#createQueueConnection(String, String)
* @see javax.jms.QueueConnectionFactory#createQueueConnection()
*/
protected QueueConnection doCreateQueueConnection(
@Nullable String username, @Nullable String password) throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
QueueConnectionFactory queueFactory = (QueueConnectionFactory) target;
if (StringUtils.hasLength(username)) {
return queueFactory.createQueueConnection(username, password);
}
else {
return queueFactory.createQueueConnection();
}
}
@Test
public void testWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
given(cf.createQueueConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createQueueConnection();
Connection con2 = scf.createQueueConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
/**
* Creates a AMQP connection with the number of channels specified, registered on top of it.
*
* @param numberOfChannels number of channels to be created using the connection
* @param userName admin user
* @param password admin password
* @param hostName localhost
* @param port the AMQP port for which the broker listens to
* @return the created JMS connection
* @throws NamingException if an error occurs while creating the context/connection factory using given properties.
* @throws JMSException if an error occurs while creating/starting the connection/session
*/
private Connection createConnection(int numberOfChannels, String userName, String password, String hostName,
String port) throws NamingException, JMSException {
InitialContext initialContext
= ClientHelper.getInitialContextBuilder(userName, password, hostName, port).build();
QueueConnectionFactory connectionFactory
= (QueueConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
QueueConnection connection = connectionFactory.createQueueConnection();
connection.start();
for (int i = 0; i < numberOfChannels; i++) {
QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
/*
For each channel, create a number of consumers that is equal to the channel number.
e.g. if the channel count is 3, channel1 has 1 consumer, channel2 has 2 consumers and channel3 has 3
consumers
*/
for (int j = 0; j < i; j++) {
Queue queue = session.createQueue("queue");
session.createReceiver(queue);
}
}
return connection;
}
@Test
public void testWithQueueConnectionFactoryAndJms11Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
given(cf.createConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createConnection();
Connection con2 = scf.createConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
@Test
public void testWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
given(cf.createQueueConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createQueueConnection();
Connection con2 = scf.createQueueConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
public QueueConnection getQueueConnection(QueueConnectionFactory qcf)
throws JMSException {
final QueueConnection qc;
final String username = Config.parms.getString("us");
if (username != null && username.length() != 0) {
Log.logger.log(Level.INFO, "getQueueConnection(): authenticating as \"" + username + "\"");
final String password = Config.parms.getString("pw");
qc = qcf.createQueueConnection(username, password);
} else {
qc = qcf.createQueueConnection();
}
return qc;
}
/**
* Send a message to testInboundQueue queue
*
* @throws Exception
*/
private void sendMessage() throws Exception {
InitialContext initialContext = JmsClientHelper.getActiveMqInitialContext();
QueueConnectionFactory connectionFactory
= (QueueConnectionFactory) initialContext.lookup(JmsClientHelper.QUEUE_CONNECTION_FACTORY);
QueueConnection queueConnection = connectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(queueSession.createQueue(QUEUE_NAME));
String message = "<?xml version='1.0' encoding='UTF-8'?>" +
" <ser:getQuote xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\"> " +
" <ser:request>" +
" <xsd:symbol>IBM</xsd:symbol>" +
" </ser:request>" +
" </ser:getQuote>";
try {
TextMessage jmsMessage = queueSession.createTextMessage(message);
jmsMessage.setJMSType("incorrecttype");
sender.send(jmsMessage);
} finally {
queueConnection.close();
}
}
@Before
public void setUp() throws Exception {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestContextFactory.class.getName());
QueueConnectionFactory queueConnectionFactory = Mockito.mock(QueueConnectionFactory.class);
Queue queue = Mockito.mock(Queue.class);
Context context = Mockito.mock(Context.class);
TestContextFactory.context = context;
when(context.lookup(eq("jms/queueConnectionFactory"))).thenReturn(queueConnectionFactory);
when(context.lookup(eq("jms/mailQueue"))).thenReturn(queue);
queueSender = Mockito.mock(QueueSender.class);
QueueConnection queueConnection = Mockito.mock(QueueConnection.class);
when(queueConnectionFactory.createQueueConnection()).thenReturn(queueConnection);
when(queueConnectionFactory.createQueueConnection(anyString(), anyString())).thenReturn(queueConnection);
QueueSession queueSession = Mockito.mock(QueueSession.class);
bytesMessage = Mockito.mock(BytesMessage.class);
when(queueSession.createBytesMessage()).thenReturn(bytesMessage);
when(queueConnection.createQueueSession(anyBoolean(), anyInt())).thenReturn(queueSession);
when(queueSession.createSender(eq(queue))).thenReturn(queueSender);
transport = new SmtpJmsTransport(Session.getDefaultInstance(new Properties()), new URLName("jms"));
transportListener = Mockito.mock(TransportListener.class);
transport.addTransportListener(transportListener);
}
protected Connection createConnection() throws JMSException {
if (StringUtils.isNotEmpty(authAlias)) {
CredentialFactory cf = new CredentialFactory(authAlias,null,null);
if (log.isDebugEnabled()) log.debug("using userId ["+cf.getUsername()+"] to create Connection");
if (useJms102()) {
if (connectionFactory instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory)connectionFactory).createQueueConnection(cf.getUsername(),cf.getPassword());
} else {
return ((TopicConnectionFactory)connectionFactory).createTopicConnection(cf.getUsername(),cf.getPassword());
}
} else {
return connectionFactory.createConnection(cf.getUsername(),cf.getPassword());
}
}
if (useJms102()) {
if (connectionFactory instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory)connectionFactory).createQueueConnection();
} else {
return ((TopicConnectionFactory)connectionFactory).createTopicConnection();
}
} else {
return connectionFactory.createConnection();
}
}
TracingConnectionFactory(Object delegate, JmsTracing jmsTracing) {
this.delegate = delegate;
this.jmsTracing = jmsTracing;
int types = 0;
if (delegate instanceof ConnectionFactory) types |= TYPE_CF;
if (delegate instanceof QueueConnectionFactory) types |= TYPE_QUEUE_CF;
if (delegate instanceof TopicConnectionFactory) types |= TYPE_TOPIC_CF;
if (delegate instanceof XAConnectionFactory) types |= TYPE_XA_CF;
if (delegate instanceof XAQueueConnectionFactory) types |= TYPE_XA_QUEUE_CF;
if (delegate instanceof XATopicConnectionFactory) types |= TYPE_XA_TOPIC_CF;
this.types = types;
}
@Override
public void lookupJMSConnectionFactory() throws TestFailureException {
try {
try {
Object obj = mdbContext.lookup("jms");
Assert.assertNotNull("The JMS ConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory);
final ConnectionFactory connectionFactory = (ConnectionFactory) obj;
testJmsConnection(connectionFactory.createConnection());
obj = mdbContext.lookup("TopicCF");
Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory);
final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
testJmsConnection(topicConnectionFactory.createConnection());
obj = mdbContext.lookup("QueueCF");
Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory);
final QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
testJmsConnection(queueConnectionFactory.createConnection());
} catch (final Exception e) {
e.printStackTrace();
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public QueueConnection createQueueConnection() throws JMSException {
try {
return ((QueueConnectionFactory) (this.connectionFactory)).createQueueConnection();
} catch (JMSException e) {
logger.error(
"JMS Exception while creating queue connection through factory '" + this.connectionFactoryString
+ "' " + e.getMessage(), e);
}
return null;
}
public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
try {
return ((QueueConnectionFactory) (this.connectionFactory)).createQueueConnection(userName, password);
} catch (JMSException e) {
logger.error(
"JMS Exception while creating queue connection through factory '" + this.connectionFactoryString
+ "' " + e.getMessage(), e);
}
return null;
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
ConnectionFactory target = getTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection();
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
ConnectionFactory target = getTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection(username, password);
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection();
}
else {
Connection con = target.createConnection();
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
return (QueueConnection) con;
}
}
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection(username, password);
}
else {
Connection con = target.createConnection(username, password);
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
return (QueueConnection) con;
}
}
/**
* Create a JMS Connection via this template's ConnectionFactory.
* @return the new JMS Connection
* @throws javax.jms.JMSException if thrown by JMS API methods
*/
protected Connection doCreateConnection() throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (Boolean.FALSE.equals(this.pubSubMode) && cf instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) cf).createQueueConnection();
}
else if (Boolean.TRUE.equals(this.pubSubMode) && cf instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) cf).createTopicConnection();
}
else {
return obtainTargetConnectionFactory().createConnection();
}
}
@Before
public void setUpMocks() throws Exception {
mockConnectionFactory = mock(QueueConnectionFactory.class);
mockConnection = mock(QueueConnection.class);
mockSession = mock(QueueSession.class);
mockQueue = mock(Queue.class);
given(mockConnectionFactory.createConnection()).willReturn(mockConnection);
given(mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)).willReturn(mockSession);
}
public void lookupJMSConnectionFactory() throws TestFailureException {
try {
try {
Object obj = getMessageDrivenContext().lookup("jms");
Assert.assertNotNull("The JMS ConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory);
final ConnectionFactory connectionFactory = (ConnectionFactory) obj;
testJmsConnection(connectionFactory.createConnection());
obj = getMessageDrivenContext().lookup("TopicCF");
Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory);
final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
testJmsConnection(topicConnectionFactory.createConnection());
obj = getMessageDrivenContext().lookup("QueueCF");
Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj);
Assert.assertTrue("Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory);
final QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
testJmsConnection(queueConnectionFactory.createConnection());
} catch (final Exception e) {
e.printStackTrace();
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
ConnectionFactory target = getTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection();
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
ConnectionFactory target = getTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection(username, password);
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection();
}
else {
Connection con = target.createConnection();
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
return (QueueConnection) con;
}
}
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection(username, password);
}
else {
Connection con = target.createConnection(username, password);
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
return (QueueConnection) con;
}
}
/**
* Create a JMS Connection via this template's ConnectionFactory.
* @return the new JMS Connection
* @throws javax.jms.JMSException if thrown by JMS API methods
*/
protected Connection doCreateConnection() throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (Boolean.FALSE.equals(this.pubSubMode) && cf instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) cf).createQueueConnection();
}
else if (Boolean.TRUE.equals(this.pubSubMode) && cf instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) cf).createTopicConnection();
}
else {
return obtainTargetConnectionFactory().createConnection();
}
}
@Before
public void setUpMocks() throws Exception {
mockConnectionFactory = mock(QueueConnectionFactory.class);
mockConnection = mock(QueueConnection.class);
mockSession = mock(QueueSession.class);
mockQueue = mock(Queue.class);
given(mockConnectionFactory.createConnection()).willReturn(mockConnection);
given(mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)).willReturn(mockSession);
}
@Test
public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
QueueConnection con = mock(QueueConnection.class);
QueueSession txSession = mock(QueueSession.class);
QueueSession nonTxSession = mock(QueueSession.class);
given(cf.createQueueConnection()).willReturn(con);
given(con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
given(txSession.getTransacted()).willReturn(true);
given(con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
CachingConnectionFactory scf = new CachingConnectionFactory(cf);
scf.setReconnectOnException(false);
Connection con1 = scf.createQueueConnection();
Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
session1.rollback();
session1.close();
session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
session1.close();
con1.start();
QueueConnection con2 = scf.createQueueConnection();
Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
session2.close();
session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
session2.getTransacted();
session2.close(); // should lead to rollback
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(txSession).rollback();
verify(txSession).close();
verify(nonTxSession).close();
verify(con).start();
verify(con).stop();
verify(con).close();
}