本文整理了Java中javax.jms.Connection.close()
方法的一些代码示例,展示了Connection.close()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Connection.close()
方法的具体详情如下:
包路径:javax.jms.Connection
类名称:Connection
方法名:close
[英]Closes the connection.
Since a provider typically allocates significant resources outside the JVM on behalf of a connection, clients should close these resources when they are not needed. Relying on garbage collection to eventually reclaim these resources may not be timely enough.
There is no need to close the sessions, producers, and consumers of a closed connection.
Closing a connection causes all temporary destinations to be deleted.
When this method is invoked, it should not return until message processing has been shut down in an orderly fashion. This means that all message listeners that may have been running have returned, and that all pending receives have returned. A close terminates all pending message receives on the connection's sessions' consumers. The receives may return with a message or with null, depending on whether there was a message available at the time of the close. If one or more of the connection's sessions' message listeners is processing a message at the time when connection close is invoked, all the facilities of the connection and its sessions must remain available to those listeners until they return control to the JMS provider.
This method must not return until any incomplete asynchronous send operations for this Connection have been completed and any CompletionListener callbacks have returned. Incomplete sends should be allowed to complete normally unless an error occurs.
For the avoidance of doubt, if an exception listener for this connection is running when close is invoked, there is no requirement for the close call to wait until the exception listener has returned before it may return.
Closing a connection causes any of its sessions' transactions in progress to be rolled back. In the case where a session's work is coordinated by an external transaction manager, a session's commit and rollback methods are not used and the result of a closed session's work is determined later by the transaction manager. Closing a connection does NOT force an acknowledgment of client-acknowledged sessions.
A message listener must not attempt to close its own connection as this would lead to deadlock. The JMS provider must detect this and throw a IllegalStateException.
A CompletionListener callback method must not call close on its own Connection. Doing so will cause an IllegalStateException to be thrown.
Invoking the acknowledge method of a received message from a closed connection's session must throw an IllegalStateException. Closing a closed connection must NOT throw an exception.
[中]关闭连接。
由于提供程序通常代表连接在JVM之外分配大量资源,因此客户端应该在不需要这些资源时关闭它们。依靠垃圾收集最终回收这些资源可能不够及时。
无需关闭已关闭连接的会话、生产者和消费者。
关闭连接会导致删除所有临时目标。
调用此方法时,在有序地关闭消息处理之前,它不应返回。这意味着可能已经运行的所有消息侦听器都已返回,并且所有挂起的接收都已返回。关闭将终止连接会话使用者上接收的所有挂起消息。根据关闭时是否有可用消息,receives可能返回消息或null。如果在调用connection close时连接的一个或多个会话的消息侦听器正在处理消息,则在这些侦听器将控制权返回JMS提供程序之前,连接及其会话的所有功能必须对这些侦听器保持可用。
在完成此连接的任何不完整异步发送操作并返回任何CompletionListener回调之前,此方法不得返回。除非发生错误,否则应允许不完整的发送正常完成。
为避免疑问,如果调用close时此连接的异常侦听器正在运行,则close调用不需要等待异常侦听器返回后才能返回。
关闭连接会导致回滚其正在进行的任何会话事务。在会话的工作由外部事务管理器协调的情况下,不使用会话的提交和回滚方法,并且关闭会话的工作结果稍后由事务管理器确定。关闭连接不会强制确认客户端已确认的会话。
消息侦听器不得尝试关闭自己的连接,因为这将导致死锁。JMS提供程序必须检测到这一点并抛出IllegalStateException。
CompletionListener回调方法不能在其自己的连接上调用close。这样做将导致抛出非法状态异常。
调用来自封闭连接会话的已接收消息的确认方法必须引发IllegalStateException。关闭关闭的连接不能引发异常。
代码示例来源:origin: kiegroup/jbpm
try {
queueConnection = connectionFactory.createConnection();
queueSession = queueConnection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
producer = queueSession.createProducer(queue);
producer.setPriority(priority);
producer.send(message);
} catch (Exception e) {
throw new RuntimeException("Error when sending JMS message with working memory event", e);
queueConnection.close();
} catch (JMSException e) {
logger.warn("Error when closing queue connection", e);
代码示例来源:origin: apache/nifi
public static WrappedMessageProducer createMessageProducer(final ProcessContext context, final boolean transacted) throws JMSException {
Connection connection = null;
Session jmsSession = null;
try {
connection = JmsFactory.createConnection(context);
jmsSession = JmsFactory.createSession(context, connection, transacted);
final Destination destination = getDestination(context);
final MessageProducer messageProducer = jmsSession.createProducer(destination);
return new WrappedMessageProducer(connection, jmsSession, messageProducer);
} catch (JMSException e) {
if (connection != null) {
connection.close();
}
if (jmsSession != null) {
jmsSession.close();
}
throw e;
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testWithConnectionFactory() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.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);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testParticipatingTransactionWithCommit() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
verify(con).close();
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testProducerCallback() throws Exception {
JmsTemplate template = createTemplate();
template.setConnectionFactory(this.connectionFactory);
MessageProducer messageProducer = mock(MessageProducer.class);
given(this.session.createProducer(null)).willReturn(messageProducer);
given(messageProducer.getPriority()).willReturn(4);
template.execute((ProducerCallback<Void>) (session1, producer) -> {
session1.getTransacted();
producer.getPriority();
return null;
});
verify(messageProducer).close();
verify(this.session).close();
verify(this.connection).close();
}
代码示例来源:origin: spring-projects/spring-framework
protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception {
JmsTemplate template = createTemplate();
template.setConnectionFactory(this.connectionFactory);
template.setMessageConverter(new SimpleMessageConverter());
String s = "Hello world";
MessageProducer messageProducer = mock(MessageProducer.class);
TextMessage textMessage = mock(TextMessage.class);
reset(this.session);
given(this.session.createProducer(this.queue)).willReturn(messageProducer);
given(this.session.createTextMessage("Hello world")).willReturn(textMessage);
willThrow(original).given(messageProducer).send(textMessage);
try {
template.convertAndSend(this.queue, s);
fail("Should have thrown JmsException");
}
catch (JmsException wrappedEx) {
// expected
assertEquals(thrownExceptionClass, wrappedEx.getClass());
assertEquals(original, wrappedEx.getCause());
}
verify(messageProducer).close();
verify(this.session).close();
verify(this.connection).close();
}
代码示例来源:origin: spring-projects/spring-framework
TemporaryQueue replyDestination = mock(TemporaryQueue.class);
MessageProducer messageProducer = mock(MessageProducer.class);
given(localSession.createProducer(this.queue)).willReturn(messageProducer);
given(localSession.createTemporaryQueue()).willReturn(replyDestination);
given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer);
verify(this.connection).start();
verify(this.connection).close();
verify(localSession).close();
verify(messageConsumer).close();
代码示例来源:origin: spring-projects/spring-framework
given(this.session.createConsumer(this.queue,
messageSelector ? selectorString : null)).willReturn(messageConsumer);
verify(this.connection).start();
verify(this.connection).close();
if (useTransactedTemplate()) {
verify(this.session).commit();
代码示例来源:origin: apache/activemq
/**
* Recreates the connection.
*
* @throws JMSException
*/
protected void reconnect() throws Exception {
if (connection != null) {
// Close the prev connection.
connection.close();
}
session = null;
connection = resourceProvider.createConnection(connectionFactory);
reconnectSession();
connection.start();
}
代码示例来源:origin: apache/nifi
private void unsubscribe(final String url, final String username, final String password, final String subscriptionId, final String jmsProvider, final int timeoutMillis) throws JMSException {
final Connection connection;
if (username == null && password == null) {
connection = JmsFactory.createConnectionFactory(url, timeoutMillis, jmsProvider).createConnection();
} else {
connection = JmsFactory.createConnectionFactory(url, timeoutMillis, jmsProvider).createConnection(username, password);
}
Session session = null;
try {
connection.setClientID(subscriptionId);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.unsubscribe(subscriptionId);
getLogger().info("Successfully unsubscribed from {}, Subscription Identifier {}", new Object[]{url, subscriptionId});
} finally {
if (session != null) {
try {
session.close();
} catch (final Exception e1) {
getLogger().warn("Unable to close session with JMS Server due to {}; resources may not be cleaned up appropriately", new Object[]{e1});
}
}
try {
connection.close();
} catch (final Exception e1) {
getLogger().warn("Unable to close connection to JMS Server due to {}; resources may not be cleaned up appropriately", new Object[]{e1});
}
}
}
代码示例来源:origin: kiegroup/jbpm
try {
queueConnection = connectionFactory.createConnection();
queueSession = queueConnection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
message.setIntProperty("EventType", eventType);
message.setStringProperty("LogType", "Process");
producer = queueSession.createProducer(queue);
producer.setPriority(priority);
producer.send(message);
} catch (Exception e) {
throw new RuntimeException("Error when sending JMS message with working memory event", e);
queueConnection.close();
} catch (JMSException e) {
logger.warn("Error when closing queue connection", e);
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testWithConnection() throws JMSException {
Connection con = mock(Connection.class);
SingleConnectionFactory scf = new SingleConnectionFactory(con);
Connection con1 = scf.createConnection();
con1.start();
con1.stop();
con1.close();
Connection con2 = scf.createConnection();
con2.start();
con2.stop();
con2.close();
scf.destroy(); // should trigger actual close
verify(con, times(2)).start();
verify(con, times(2)).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testTransactionCommit() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
final Session session = mock(Session.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
JmsTemplate jt = new JmsTemplate(cf);
jt.execute(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
tm.commit(ts);
verify(session).commit();
verify(session).close();
verify(con).close();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception {
JmsTemplate template = createTemplate();
template.setConnectionFactory(this.connectionFactory);
template.setMessageIdEnabled(false);
template.setMessageTimestampEnabled(false);
MessageProducer messageProducer = mock(MessageProducer.class);
given(this.session.createProducer(null)).willReturn(messageProducer);
given(messageProducer.getPriority()).willReturn(4);
template.execute((ProducerCallback<Void>) (session1, producer) -> {
session1.getTransacted();
producer.getPriority();
return null;
});
verify(messageProducer).setDisableMessageID(true);
verify(messageProducer).setDisableMessageTimestamp(true);
verify(messageProducer).close();
verify(this.session).close();
verify(this.connection).close();
}
代码示例来源:origin: spring-projects/spring-framework
TextMessage textMessage = mock(TextMessage.class);
given(this.session.createProducer(this.queue)).willReturn(messageProducer);
given(this.session.createTextMessage("just testing")).willReturn(textMessage);
verify(messageProducer).send(textMessage);
verify(messageProducer).send(textMessage, this.qosSettings.getDeliveryMode(),
this.qosSettings.getPriority(), this.qosSettings.getTimeToLive());
verify(this.connection).close();
代码示例来源:origin: spring-projects/spring-framework
Connection con = resourceFactory.getConnection(resourceHolder);
if (con != null) {
con.start();
resourceHolderToUse.addSession(session, con);
if (startConnection) {
con.start();
con.close();
代码示例来源:origin: kiegroup/jbpm
public List<Message> receive(Queue queue) throws Exception {
List<Message> messages = new ArrayList<Message>();
Connection qconnetion = factory.createConnection();
Session qsession = qconnetion.createSession(true, QueueSession.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = qsession.createConsumer(queue);
qconnetion.start();
Message m = null;
while ((m = consumer.receiveNoWait()) != null) {
messages.add(m);
}
consumer.close();
qsession.close();
qconnetion.close();
return messages;
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testWithConnectionFactoryAndClientId() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
given(cf.createConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
scf.setClientId("myId");
Connection con1 = scf.createConnection();
Connection con2 = scf.createConnection();
con1.start();
con2.start();
con1.close();
con2.close();
scf.destroy(); // should trigger actual close
verify(con).setClientID("myId");
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testTransactionRollback() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
final Session session = mock(Session.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
JmsTemplate jt = new JmsTemplate(cf);
jt.execute(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
tm.rollback(ts);
verify(session).rollback();
verify(session).close();
verify(con).close();
}
代码示例来源:origin: apache/activemq
try {
connection = cf.createConnection(userName, password);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(dest);
ActiveMQTextMessage msg = (ActiveMQTextMessage) session.createTextMessage(body);
producer.send(msg);
connection.close();
内容来源于网络,如有侵权,请联系作者删除!