javax.jms.QueueConnectionFactory Java Examples

The following examples show how to use javax.jms.QueueConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingleConnectionFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: JMSConnectionFactory.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: MessagingSource.java    From iaf with Apache License 2.0 6 votes vote down vote up
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();
	}
}
 
Example #4
Source File: CloseCmdTest.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: AbstractJMSProvider.java    From perf-harness with MIT License 6 votes vote down vote up
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;

}
 
Example #6
Source File: UserCredentialsConnectionFactoryAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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();
	}
}
 
Example #7
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: SingleConnectionFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #9
Source File: JmsTypeHeaderInboundEndpointTestCase.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example #10
Source File: SingleConnectionFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #11
Source File: SingleConnectionFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #12
Source File: SingleConnectionFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #13
Source File: ConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Test that ConnectionFactory can be cast to QueueConnectionFactory and QueueConnection can be
 * created.
 */
@Test
public void testQueueConnectionFactory() throws Exception {
   deployConnectionFactory(0, JMSFactoryType.QUEUE_CF, "CF_QUEUE_XA_FALSE", "/CF_QUEUE_XA_FALSE");
   QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup("/CF_QUEUE_XA_FALSE");
   QueueConnection qc = qcf.createQueueConnection();
   qc.close();
   undeployConnectionFactory("CF_QUEUE_XA_FALSE");
}
 
Example #14
Source File: JMSWrapper.java    From core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Dynamically creates a topic. This goes against the normal idea
    * that JMS queues and topics should managed administratively, using
    * management tools. But for some applications this would be too 
    * burdensome. The user would have to additionally know about the
    * administration tools as well. Given that might be creating quite
    * a few AVL feeds, each one being a separate topic, this could be
    * a real nuisance.
    * 
 * @param topicName
 * @return true if topic created successfully
 * @throws JMSException
 */
private boolean createTopic(String topicName) throws JMSException {
	QueueConnectionFactory queueConnectionFactory = 
			(QueueConnectionFactory) connectionFactory;
	QueueConnection connection = queueConnectionFactory.createQueueConnection();

	Queue managementQueue = HornetQJMSClient.createQueue("hornetq.management");
	QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
	connection.start();
	Message message = session.createMessage();
	JMSManagementHelper.putOperationInvocation(message, 
			"jms.server", 
			"createTopic", // management command
			topicName,     // Name in hornetq
			topicName);    // JNDI name. This peculiar seemingly undocumented 
						   // parameter is needed so that can use JNDI to access 
	                       // the dynamically created topic. Found info on doing 
	                       // this at https://community.jboss.org/thread/165355 .
	QueueRequestor requestor = new QueueRequestor(session, managementQueue);

	// Determine if was successful
	Message reply = requestor.request(message);
	boolean topicCreated = JMSManagementHelper.hasOperationSucceeded(reply);
	
	if (topicCreated)
		logger.info("Dynamically created topic \"" + topicName + "\"");
	else
		logger.error("Failed to dynamically created topic \"" + topicName + "\"");
	
	// Return whether successful
	return topicCreated;
}
 
Example #15
Source File: EncBmpBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            Object obj = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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);
    }
}
 
Example #16
Source File: ConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void assertNTypes(ActiveMQConnectionFactory factory, final int total) {
   StringBuilder text = new StringBuilder();
   text.append(factory + "\n is instance of ");
   int num = 0;
   if (factory instanceof ConnectionFactory) {
      num++;
      text.append("ConnectionFactory ");
   }
   if (factory instanceof XAConnectionFactory) {
      num++;
      text.append("XAConnectionFactory ");
   }
   if (factory instanceof QueueConnectionFactory) {
      num++;
      text.append("QueueConnectionFactory ");
   }
   if (factory instanceof TopicConnectionFactory) {
      num++;
      text.append("TopicConnectionFactory ");
   }
   if (factory instanceof XAQueueConnectionFactory) {
      num++;
      text.append("XAQueueConnectionFactory ");
   }
   if (factory instanceof XATopicConnectionFactory) {
      num++;
      text.append("XATopicConnectionFactory ");
   }
   Assert.assertEquals(text.toString(), total, num);
}
 
Example #17
Source File: JNDIContext.java    From product-cep with Apache License 2.0 5 votes vote down vote up
private void createQueueConnectionFactory() {
    // create queue connection factory
    try {
        queueConnectionFactory = (QueueConnectionFactory) initContext.lookup("ConnectionFactory");
    } catch (NamingException e) {
        log.info("Can not create queue connection factory." + e);
    }
}
 
Example #18
Source File: PTPTestCase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Create all administrated objects connections and sessions ready to use for tests.
 * <br />
 * Start connections.
 */
@Override
@Before
public void setUp() throws Exception {
   super.setUp();

   try {
      // ...and creates administrated objects and binds them
      admin.createQueueConnectionFactory(PTPTestCase.QCF_NAME);
      admin.createQueue(PTPTestCase.QUEUE_NAME);

      Context ctx = admin.createContext();

      senderQCF = (QueueConnectionFactory) ctx.lookup(PTPTestCase.QCF_NAME);
      senderQueue = (Queue) ctx.lookup(PTPTestCase.QUEUE_NAME);
      senderConnection = senderQCF.createQueueConnection();
      senderSession = senderConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      sender = senderSession.createSender(senderQueue);

      receiverQCF = (QueueConnectionFactory) ctx.lookup(PTPTestCase.QCF_NAME);
      receiverQueue = (Queue) ctx.lookup(PTPTestCase.QUEUE_NAME);
      receiverConnection = receiverQCF.createQueueConnection();
      receiverSession = receiverConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      receiver = receiverSession.createReceiver(receiverQueue);

      senderConnection.start();
      receiverConnection.start();
      // end of client step
   } catch (Exception e) {
      throw new RuntimeException(e);
   }
}
 
Example #19
Source File: EncStatefulBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            Object obj = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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);
    }
}
 
Example #20
Source File: ContextLookupStatefulPojoBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            Object obj = ejbContext.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 = ejbContext.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 = ejbContext.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);
    }
}
 
Example #21
Source File: EncStatelessBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            Object obj = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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 = ctx.lookup("java:comp/env/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);
    }
}
 
Example #22
Source File: UserCredentialsConnectionFactoryAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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(String username, String password) throws JMSException {
	Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
	if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
		throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
	}
	QueueConnectionFactory queueFactory = (QueueConnectionFactory) this.targetConnectionFactory;
	if (StringUtils.hasLength(username)) {
		return queueFactory.createQueueConnection(username, password);
	}
	else {
		return queueFactory.createQueueConnection();
	}
}
 
Example #23
Source File: SingleConnectionFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 getTargetConnectionFactory().createConnection();
	}
}
 
Example #24
Source File: DelegatingConnectionFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
	ConnectionFactory cf = getTargetConnectionFactory();
	if (cf instanceof QueueConnectionFactory) {
		return ((QueueConnectionFactory) cf).createQueueConnection(username, password);
	}
	else {
		Connection con = cf.createConnection(username, password);
		if (!(con instanceof QueueConnection)) {
			throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
		}
		return (QueueConnection) con;
	}
}
 
Example #25
Source File: DelegatingConnectionFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public QueueConnection createQueueConnection() throws JMSException {
	ConnectionFactory cf = getTargetConnectionFactory();
	if (cf instanceof QueueConnectionFactory) {
		return ((QueueConnectionFactory) cf).createQueueConnection();
	}
	else {
		Connection con = cf.createConnection();
		if (!(con instanceof QueueConnection)) {
			throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
		}
		return (QueueConnection) con;
	}
}
 
Example #26
Source File: TransactionAwareConnectionFactoryProxy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
	if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
		throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
	}
	QueueConnection targetConnection =
			((QueueConnectionFactory) this.targetConnectionFactory).createQueueConnection(username, password);
	return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
 
Example #27
Source File: TransactionAwareConnectionFactoryProxy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public QueueConnection createQueueConnection() throws JMSException {
	if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
		throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
	}
	QueueConnection targetConnection =
			((QueueConnectionFactory) this.targetConnectionFactory).createQueueConnection();
	return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
 
Example #28
Source File: TracingConnectionFactory.java    From brave with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: ContextLookupSingletonBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            Object obj = ejbContext.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 = ejbContext.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 = ejbContext.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);
    }
}
 
Example #30
Source File: JmsQueueListener.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public synchronized void load() throws GenericServiceException {
    try {
        InitialContext jndi = JNDIContextFactory.getInitialContext(jndiServer);
        QueueConnectionFactory factory = (QueueConnectionFactory) jndi.lookup(jndiName);

        if (factory != null) {
            con = factory.createQueueConnection(userName, password);
            con.setExceptionListener(this);
            session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            queue = (Queue) jndi.lookup(queueName);
            if (queue != null) {
                QueueReceiver receiver = session.createReceiver(queue);

                receiver.setMessageListener(this);
                con.start();
                this.setConnected(true);
                Debug.logInfo("Listening to queue [" + queueName + "]...", module);
            } else {
                throw new GenericServiceException("Queue lookup failed.");
            }
        } else {
            throw new GenericServiceException("Factory (broker) lookup failed.");
        }
    } catch (NamingException ne) {
        throw new GenericServiceException("JNDI lookup problems; listener not running.", ne);
    } catch (JMSException je) {
        throw new GenericServiceException("JMS internal error; listener not running.", je);
    } catch (GeneralException ge) {
        throw new GenericServiceException("Problems with InitialContext; listener not running.", ge);
    }
}