本文整理汇总了Java中javax.jms.Connection.start方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.start方法的具体用法?Java Connection.start怎么用?Java Connection.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.Connection
的用法示例。
在下文中一共展示了Connection.start方法的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: testFailedCreateConsumerConnectionStillWorks
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testFailedCreateConsumerConnectionStillWorks() throws JMSException {
Connection connection = pooledConnFact.createConnection("guest", "password");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(name.getMethodName());
try {
session.createConsumer(queue);
fail("Should fail to create consumer");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
queue = session.createQueue("GUESTS." + name.getMethodName());
MessageProducer producer = session.createProducer(queue);
producer.close();
connection.close();
}
示例3: 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.");
}
示例4: 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.");
}
示例5: sendMessages
import javax.jms.Connection; //导入方法依赖的package包/类
public void sendMessages(ConnectionFactory connectionFactory) throws Exception {
for (int i = 0; i < NUM_MESSAGES; i++) {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE);
MessageProducer producer = session.createProducer(destination);
String msgTo = "hello";
TextMessage message = session.createTextMessage(msgTo);
producer.send(message);
connection.close();
LOG.debug("sent " + i + " messages using " + connectionFactory.getClass());
}
}
示例6: testFailedConnectThenSucceeds
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testFailedConnectThenSucceeds() throws JMSException {
Connection connection = pooledConnFact.createConnection("invalid", "credentials");
try {
connection.start();
fail("Should fail to connect");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
connection = pooledConnFact.createConnection("system", "manager");
connection.start();
LOG.info("Successfully create new connection.");
connection.close();
}
示例7: testFailedConnectThenSucceedsWithListener
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testFailedConnectThenSucceedsWithListener() throws JMSException {
Connection connection = pooledConnFact.createConnection("invalid", "credentials");
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.warn("Connection get error: {}", exception.getMessage());
}
});
try {
connection.start();
fail("Should fail to connect");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
connection = pooledConnFact.createConnection("system", "manager");
connection.start();
LOG.info("Successfully create new connection.");
connection.close();
}
示例8: 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
JmsPoolConnectionFactory 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 {
cf.stop();
}
LOG.debug("Test finished.");
}
示例9: testSetClientIDAfterConnectionStart
import javax.jms.Connection; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
LOG.debug("running testRepeatedSetClientIDCalls()");
JmsPoolConnectionFactory 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();
cf.stop();
}
LOG.debug("Test finished.");
}
示例10: validateFactoryCreationWithActiveMQLibraries
import javax.jms.Connection; //导入方法依赖的package包/类
/**
* This test simply validates that {@link ConnectionFactory} can be setup by
* pointing to the location of the client libraries at runtime. It uses
* ActiveMQ which is not present at the POM but instead pulled from Maven
* repo using {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which
* implies that for this test to run the computer must be connected to the
* Internet. If computer is not connected to the Internet, this test will
* quietly fail logging a message.
*/
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
try {
String libPath = TestUtils.setupActiveMqLibForTesting(true);
TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
runner.addControllerService("cfProvider", cfProvider);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI,
"vm://localhost?broker.persistent=false");
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
"org.apache.activemq.ActiveMQConnectionFactory");
runner.enableControllerService(cfProvider);
runner.assertValid(cfProvider);
Connection connection = cfProvider.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("myqueue");
MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello");
producer.send(message);
assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
connection.stop();
connection.close();
} catch (Exception e) {
logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
}
}
示例11: validateFactoryCreationWithActiveMQLibraries
import javax.jms.Connection; //导入方法依赖的package包/类
/**
* This test simply validates that {@link ConnectionFactory} can be setup by pointing to the location of the client
* libraries at runtime. It uses ActiveMQ which is not present at the POM but instead pulled from Maven repo using
* {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which implies that for this test to run the computer must
* be connected to the Internet. If computer is not connected to the Internet, this test will quietly fail logging a
* message.
*/
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
try {
String libPath = TestUtils.setupActiveMqLibForTesting(true);
TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
JNDIConnectionFactoryProvider cfProvider = new JNDIConnectionFactoryProvider();
runner.addControllerService("cfProvider", cfProvider);
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.BROKER_URI,
"vm://localhost?broker.persistent=false");
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.JNDI_CF_LOOKUP, "ConnectionFactory");
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
runner.enableControllerService(cfProvider);
runner.assertValid(cfProvider);
Connection connection = cfProvider.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("myqueue");
MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello");
producer.send(message);
assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
connection.stop();
connection.close();
} catch (Exception e) {
logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
}
}
示例12: run
import javax.jms.Connection; //导入方法依赖的package包/类
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("HELLOWORLD.TESTQ");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
示例13: createConnectionAndReceiveMessage
import javax.jms.Connection; //导入方法依赖的package包/类
public void createConnectionAndReceiveMessage(String clientId, String ipAddress) throws JMSException {
Connection connection = connectionFactory.createConnection();
connection.setClientID(clientId);
connection.start();
Session session = connection.createSession(false, AUTO_ACKNOWLEDGE);
String selector = "s_id = 'Sample'";
logger.info("selector : '" + selector + "'....");
TopicSubscriber consumer = session.createDurableSubscriber(topic, "Sub1", selector, true);
consumer.setMessageListener(topicMessageListener);
}
示例14: getHeartbeats
import javax.jms.Connection; //导入方法依赖的package包/类
/**
*
* @param uri
* @param topicName
* @param clazz
* @param monitorTime
* @return
*/
public Map<String, T> getHeartbeats(final URI uri, final String topicName, final Class<T> clazz, final long monitorTime) throws Exception {
final Map<String, T> ret = new HashMap<String, T>(3);
Connection topicConnection = null;
try {
ConnectionFactory connectionFactory = (ConnectionFactory)service.createConnectionFactory(uri);
topicConnection = connectionFactory.createConnection();
topicConnection.start();
Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
final MessageConsumer consumer = session.createConsumer(topic);
MessageListener listener = new MessageListener() {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage t = (TextMessage) message;
final T bean = (T)service.unmarshal(t.getText(), clazz);
Method nameMethod = bean.getClass().getMethod("getName");
ret.put((String)nameMethod.invoke(bean), bean);
}
} catch (Exception e) {
logger.error("Updating changed bean from topic", e);
}
}
};
consumer.setMessageListener(listener);
Thread.sleep(monitorTime);
return ret;
} catch (Exception ne) {
logger.error("Cannot listen to topic changes because command server is not there", ne);
return null;
} finally {
topicConnection.close();
}
}
示例15: createConnectionAndSendMessage
import javax.jms.Connection; //导入方法依赖的package包/类
public void createConnectionAndSendMessage(String ipAddress) throws JMSException {
Connection connection = connectionFactory.createConnection();
connection.start();
Session topicSession = connection.createSession(false, AUTO_ACKNOWLEDGE);
MessageProducer producer = topicSession.createProducer(topic);
producer.setDeliveryMode(PERSISTENT);
ObjectMessage message = topicSession.createObjectMessage();
BUSStop busStop = new BUSStop();
busStop.setName("Rome");
message.setStringProperty("s_id", "Sample");
message.setObject((Serializable) busStop);
producer.send(message);
logger.info("message sent successfully");
}