use of javax.jms.Connection in project hive by apache.
the class NotificationListener method createConnection.
/**
* Create the JMS connection
* @return newly created JMS connection
*/
protected Connection createConnection() {
LOG.info("Will create new JMS connection");
Context jndiCntxt;
Connection jmsConnection = null;
try {
jndiCntxt = new InitialContext();
ConnectionFactory connFac = (ConnectionFactory) jndiCntxt.lookup("ConnectionFactory");
jmsConnection = connFac.createConnection();
jmsConnection.start();
jmsConnection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException jmse) {
LOG.error("JMS Exception listener received exception. Ignored the error", jmse);
}
});
} catch (NamingException e) {
LOG.error("JNDI error while setting up Message Bus connection. " + "Please make sure file named 'jndi.properties' is in " + "classpath and contains appropriate key-value pairs.", e);
} catch (JMSException e) {
LOG.error("Failed to initialize connection to message bus", e);
} catch (Throwable t) {
LOG.error("Unable to connect to JMS provider", t);
}
return jmsConnection;
}
use of javax.jms.Connection in project quickstarts by jboss-switchyard.
the class JMSClient method sendToActiveMQ.
private static void sendToActiveMQ(String payload, String queueName) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASSWD, AMQ_BROKER_URL);
Connection conn = cf.createConnection();
conn.start();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(queueName));
producer.send(session.createTextMessage(payload));
session.close();
verifyOutputQueue(conn.createSession(false, Session.AUTO_ACKNOWLEDGE));
} finally {
conn.close();
}
}
use of javax.jms.Connection in project quickstarts by jboss-switchyard.
the class JmsBindingTest method testRollbackA.
/**
* Triggers the 'WorkService' by sending a HornetQ Message to the 'policyQSTransacted' queue.
*/
@Test
public void testRollbackA() throws Exception {
String command = "rollback.A";
Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
conn.start();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(_queueIn);
TextMessage message = session.createTextMessage();
message.setText(command);
producer.send(message);
session.close();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(_queueOutA);
TextMessage msg = TextMessage.class.cast(consumer.receive(30000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
consumer.close();
consumer = session.createConsumer(_queueOutB);
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
consumer.close();
consumer = session.createConsumer(_queueOutC);
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
session.close();
} finally {
conn.close();
}
}
use of javax.jms.Connection in project javaee7-samples by javaee-samples.
the class ClassicMessageSender method sendMessage.
public void sendMessage(String payload) {
Connection connection = null;
try {
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(demoQueue);
TextMessage textMessage = session.createTextMessage(payload);
messageProducer.send(textMessage);
} catch (JMSException ex) {
ex.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}
}
use of javax.jms.Connection in project camel by apache.
the class ConnectionResourceIT method testCreateConnections.
/**
* Test method for
* {@link org.apache.commons.pool.ObjectPool#returnObject(java.lang.Object)}
* .
*
* @throws Exception
*/
@Test
public void testCreateConnections() throws Exception {
ConnectionResource pool = new AMQConnectionResource("tcp://localhost:33333", 1);
assertNotNull(pool);
Connection connection = pool.borrowConnection();
assertNotNull(connection);
assertNotNull(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
pool.returnConnection(connection);
Connection connection2 = pool.borrowConnection();
assertNotNull(connection2);
}
Aggregations