javax.jms.Connection.stop()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(338)

本文整理了Java中javax.jms.Connection.stop()方法的一些代码示例,展示了Connection.stop()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Connection.stop()方法的具体详情如下:
包路径:javax.jms.Connection
类名称:Connection
方法名:stop

Connection.stop介绍

[英]Temporarily stops a connection's delivery of incoming messages. Delivery can be restarted using the connection's start method. When the connection is stopped, delivery to all the connection's message consumers is inhibited: synchronous receives block, and messages are not delivered to message listeners.

This call blocks until receives and/or message listeners in progress have completed.

Stopping a connection has no effect on its ability to send messages. A call to stop on a connection that has already been stopped is ignored.

A call to stop must not return until delivery of messages has paused. This means that a client can rely on the fact that none of its message listeners will be called and that all threads of control waiting for receive calls to return will not return with a message until the connection is restarted. The receive timers for a stopped connection continue to advance, so receives may time out while the connection is stopped.

If message listeners are running when stop is invoked, the stop call must wait until all of them have returned before it may return. While these message listeners are completing, they must have the full services of the connection available to them.

A message listener must not attempt to stop its own connection as this would lead to deadlock. The JMS provider must detect this and throw a IllegalStateException.

For the avoidance of doubt, if an exception listener for this connection is running when stop is invoked, there is no requirement for the stop call to wait until the exception listener has returned before it may return.

This method must not be used in a Java EE web or EJB application. Doing so may cause a JMSException to be thrown though this is not guaranteed.
[中]暂时停止连接传入消息的传递。可以使用连接的start方法重新启动传递。当连接停止时,将禁止向连接的所有消息使用者传递消息:同步接收块,消息不会传递给消息侦听器。
此调用将一直阻止,直到正在进行的接收和/或消息侦听器完成为止。
停止连接不会影响其发送消息的能力。对已停止的连接调用stop将被忽略。
在消息传递暂停之前,停止调用不得返回。这意味着客户端可以依赖这样一个事实:它的消息侦听器都不会被调用,并且等待接收调用返回的所有控制线程在连接重新启动之前不会返回消息。已停止连接的接收计时器继续前进,因此在连接停止时接收可能超时。
如果调用stop时消息侦听器正在运行,则stop调用必须等到所有侦听器都返回后才能返回。当这些消息侦听器完成时,它们必须拥有连接的全部服务。
消息侦听器不得尝试停止自己的连接,因为这将导致死锁。JMS提供程序必须检测到这一点并抛出IllegalStateException。
为避免疑问,如果调用stop时此连接的异常侦听器正在运行,则stop调用不需要等待异常侦听器返回后才能返回。
此方法不得用于JavaEEWeb或EJB应用程序中。这样做可能会导致抛出JMSException,但这并不能保证。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void localStop() throws JMSException {
  synchronized (connectionMonitor) {
    if (this.locallyStarted) {
      this.locallyStarted = false;
      if (startedCount == 1 && connection != null) {
        connection.stop();
      }
      if (startedCount > 0) {
        startedCount--;
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Stop the shared Connection.
 * @throws JMSException if thrown by JMS API methods
 * @see javax.jms.Connection#start()
 */
protected void stopSharedConnection() throws JMSException {
  synchronized (this.sharedConnectionMonitor) {
    this.sharedConnectionStarted = false;
    if (this.sharedConnection != null) {
      try {
        this.sharedConnection.stop();
      }
      catch (javax.jms.IllegalStateException ex) {
        logger.debug("Ignoring Connection stop exception - assuming already stopped: " + ex);
      }
    }
  }
}

代码示例来源:origin: openzipkin/brave

@Override public void stop() throws JMSException {
 delegate.stop();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Close the given Connection.
 * @param con the Connection to close
 */
protected void closeConnection(Connection con) {
  if (logger.isDebugEnabled()) {
    logger.debug("Closing shared JMS Connection: " + con);
  }
  try {
    try {
      if (this.startedCount > 0) {
        con.stop();
      }
    }
    finally {
      con.close();
    }
  }
  catch (javax.jms.IllegalStateException ex) {
    logger.debug("Ignoring Connection state exception - assuming already closed: " + ex);
  }
  catch (Throwable ex) {
    logger.debug("Could not close shared JMS Connection", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

if (stop) {
  try {
    con.stop();

代码示例来源:origin: spring-projects/spring-framework

con.stop();

代码示例来源:origin: apache/rocketmq-externals

/**
 * Close the given Connection.
 *
 * @param con the Connection to close
 */
protected void closeConnection(Connection con) {
  logger.debug("Closing shared JMS Connection: {}", this.connection);
  try {
    try {
      con.stop();
    }
    finally {
      con.close();
    }
  }
  catch (Throwable ex) {
    logger.error("Could not close shared JMS Connection.", ex);
  }
}

代码示例来源:origin: apache/ignite

/**
 * Stops streamer.
 */
public void stop() throws IgniteException {
  if (stopped)
    throw new IgniteException("Attempted to stop an already stopped JMS Streamer");
  try {
    stopped = true;
    if (scheduler != null && !scheduler.isShutdown()) {
      scheduler.shutdown();
      scheduler = null;
    }
    connection.stop();
    connection.close();
    for (Session s : sessions) {
      s.close();
    }
    sessions.clear();
    consumers.clear();
    listeners.clear();
  }
  catch (Throwable t) {
    throw new IgniteException("Exception while stopping JmsStreamer", t);
  }
}

代码示例来源: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 testWithConnectionAggregatedStartStop() throws JMSException {
  Connection con = mock(Connection.class);
  SingleConnectionFactory scf = new SingleConnectionFactory(con);
  Connection con1 = scf.createConnection();
  con1.start();
  verify(con).start();
  con1.stop();
  verify(con).stop();
  Connection con2 = scf.createConnection();
  con2.start();
  verify(con, times(2)).start();
  con2.stop();
  verify(con, times(2)).stop();
  con2.start();
  verify(con, times(3)).start();
  con1.start();
  con2.stop();
  con1.stop();
  verify(con, times(3)).stop();
  con1.start();
  verify(con, times(4)).start();
  con1.close();
  verify(con, times(4)).stop();
  con2.close();
  scf.destroy();
  verify(con).close();
  verifyNoMoreInteractions(con);
}

代码示例来源: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 testWithConnectionFactoryAndExceptionListener() throws JMSException {
  ConnectionFactory cf = mock(ConnectionFactory.class);
  Connection con = mock(Connection.class);
  ExceptionListener listener = new ChainedExceptionListener();
  given(cf.createConnection()).willReturn(con);
  given(con.getExceptionListener()).willReturn(listener);
  SingleConnectionFactory scf = new SingleConnectionFactory(cf);
  scf.setExceptionListener(listener);
  Connection con1 = scf.createConnection();
  assertEquals(listener, con1.getExceptionListener());
  con1.start();
  con1.stop();
  con1.close();
  Connection con2 = scf.createConnection();
  con2.start();
  con2.stop();
  con2.close();
  scf.destroy();  // should trigger actual close
  verify(con).setExceptionListener(listener);
  verify(con, times(2)).start();
  verify(con, times(2)).stop();
  verify(con).close();
}

代码示例来源: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

verify(nonTxSession).close();
verify(con).start();
verify(con).stop();
verify(con).close();

代码示例来源:origin: apache/activemq

@Test
public void testAMQ5050DefaultHost() throws Exception {
  // Let verify a host header is added to the connection.
  Connection connection = new ActiveMQConnectionFactory(brokerConnectURI).createConnection();
  connection.start();
  CopyOnWriteArrayList<TransportConnection> connections = broker.getConnectorByName("tcp").getConnections();
  assertEquals(1, connections.size());
  assertNotNull(connections.get(0).getRemoteWireFormatInfo().getHost());
  connection.stop();
}

代码示例来源:origin: apache/activemq

@Test
public void testAMQ5050WithManualSpecifiedHost() throws Exception {
  // Let verify a host header is added to the connection.
  Connection connection = new ActiveMQConnectionFactory(brokerConnectURI+"?wireFormat.host=foo").createConnection();
  connection.start();
  CopyOnWriteArrayList<TransportConnection> connections = broker.getConnectorByName("tcp").getConnections();
  assertEquals(1, connections.size());
  assertEquals("foo", connections.get(0).getRemoteWireFormatInfo().getHost());
  connection.stop();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testWithQueueConnection() throws JMSException {
  Connection con = mock(QueueConnection.class);
  SingleConnectionFactory scf = new SingleConnectionFactory(con);
  QueueConnection con1 = scf.createQueueConnection();
  con1.start();
  con1.stop();
  con1.close();
  QueueConnection con2 = scf.createQueueConnection();
  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 testWithTopicConnection() throws JMSException {
  Connection con = mock(TopicConnection.class);
  SingleConnectionFactory scf = new SingleConnectionFactory(con);
  TopicConnection con1 = scf.createTopicConnection();
  con1.start();
  con1.stop();
  con1.close();
  TopicConnection con2 = scf.createTopicConnection();
  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

verify(this.connection).stop();
verify(this.connection).close();

代码示例来源:origin: mercyblitz/segmentfault-lessons

private static void receiveMessage() throws Exception {
  // 创建 ActiveMQ 链接,设置 Broker URL
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  // 创造 JMS 链接
  Connection connection = connectionFactory.createConnection();
  // 启动连接
  connection.start();
  // 创建会话 Session
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  // 创建消息目的 - Queue 名称为 "TEST"
  Destination destination = session.createQueue("TEST");
  // 创建消息消费者
  MessageConsumer messageConsumer = session.createConsumer(destination);
  // 获取消息
  Message message = messageConsumer.receive(100);
  if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    System.out.println("消息消费内容:" + textMessage.getText());
  }
  // 关闭消息消费者
  messageConsumer.close();
  // 关闭会话
  session.close();
  // 关闭连接
  connection.stop();
  connection.close();
}

相关文章