2011-10-18 2 views
7

ActiveMQ 브로커의 상태를 확인하기위한 유일한 목적 인 Java 클래스를 만들려고합니다 (또는 중단으로 인해 ActiveMQ 브로커에 대한 연결이 클라이언트 연결 네트워크 연결 끊김으로 정의 될 수 있음). 게다가).ActiveMQ 브로커의 상태 확인

그래서 기본적으로 브로커의 상태를 확인하기 위해 매초마다 실행되는 스레드가 있으며 브로커가 다운 된 경우 지원 그룹에 우편을 보내는 작업을하고 싶습니다.

온라인 예제는 위의 방법을 설명하는 데 충분하지 않습니다.

누군가 이미이 작업을 수행 했습니까? 아니면이 작업을 수행 할 수있는 좋은 방법을 제안 할 수 있습니까?

덕분에, Neeraj

답변

6

브로커에 Testmessage 보내기 :

try { 
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); 
    Connection conn = factory.createConnection(user, password); 
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 
    MessageProducer producer = session.createProducer("test"); 
    MessageConsumer consumer = session.createConsumer("test"); 
    consumer.setMessageListener(this); // class that implements MessageListener 
    conn.start(); 
    TextMessage message = new ActiveMQTextMessage(); 
    message.setText("TestMessage"); 
    producer.send(message); 
} catch (JMSException e) { 
    // somethings very wrong 
} 

연결, 당신은 메시지를받을 경우, 메시지를 보낼 : 모든 괜찮습니다. 그렇지 않다면 ...

thats 내가하는 일. 또한 다른 것들을 수행합니다 :

  • 몇 가지 자문 주제를 듣고 중요한 일들 (예 : Advisory.FULL)을 듣는 것이 중요하다고 생각하는 중요한 지표입니다.
  • 은 정기적으로 통계 플러그인에서 브로커 통계를 가져 와서 메시지 메모리 크기 및 메시지 저장소를 모니터링합니다.
  • 메시지가 소비자에 의해 거부되었을 때 알 수 있도록 배달 못 한 편지 큐를 구성하십시오.
+0

브로커가 다운 되어도 응답하지 않거나 막대한 네트워크 지연이있는 경우에도 catch 블록이 실행됩니까? – Neeraj

+0

브로커가 다운되면 createConnection 호출이 실패하고 예외가 발견됩니다. 네트워크 지연을 확인하려면 원격 대상에서 수동으로 서버에 ping을 수행해야합니다.루프백 장치가 결코 느려지지 않습니다. – Laures

7

다음은 ActiveMQ를가 시작되어 실행되고 있는지 확인하기 위해 노력할 것 : 나는 같은 질문에 대한 솔루션이 필요

try { 
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url); 
    // set transport listener so that active MQ start is notified. 
    factory.setTransportListener(transportListenerObject); 
    Connection connection = factory.createConnection(); 
    // This will throw error if activeMQ is not running. 
    connection.setClientID("my_client_id"); 
} catch (JMSException ex) { 
    if (ex.getLinkedException() instanceof IOException) { 
     // ActiveMQ is not running. Do some logic here. 
     // use the TransportListener to restart the activeMQ connection 
     // when activeMQ comes back up. 
    } else { 
     // Something seriously went wrong with the factory or connection 
     // creation. Abort the process here, as nothing can be done. 
     // Log the error and troubleshoot. 
    } 
} 
+0

왜 이것을 다운 그레이드 했습니까? – Anand

+0

어떻게 ActiveMQ 브로커가 다시 돌아올 때 TransportListener를 사용하여 재시작 할 수 있습니까? – CoderX

7

, 나는 그것에 대해 더 읽고 몇 가지 테스트를 만든 이유가 있습니다.

일부 환경에서는 테스트 메시지를 보내면 (Laures) 문제가 될 수 있습니다.

"정상적인"방법은 TransportListener (Anand 제안)를 설정하는 것이지만 제공된 인터페이스를 실제로 구현하고보고 된 이벤트에 반응합니다.

다른 ActiveMQ 초보자를 위해 (지난 달까지) 저는 샘플 시작 구현을 게시합니다. 그냥 각 이벤트에 대한 로깅을 씁니다. 실제 환경에서 하나 ... transportResumed() 또는 이와 유사한 많은 것을 더 때까지 transportInterupted()에 재 연결 재판에 대해

import java.io.IOException; 

import org.apache.activemq.transport.TransportListener; 
import org.apache.log4j.Logger; 

class ConnectionStateMonitor 
    implements TransportListener 
{ 
    private static final Logger log = Logger.getLogger(ConnectionStateMonitor.class); 

    @Override 
    public void onCommand(Object command) 
    { 
    log.debug("Command detected: '" + command + "'"); 
    } 

    @Override 
    public void onException(IOException exception) 
    { 
    log.error("Exception detected: '" + exception + "'"); 
    } 

    @Override 
    public void transportInterupted() 
    { 
    log.error("Transport interuption detected."); 
    } 

    @Override 
    public void transportResumed() 
    { 
    log.info("Transport resumption detected."); 
    } 
} 

TransportListener을 생각할 수하는 것은 설정할 수 있습니다 예컨대 :

ActiveMQConnection connection = (ActiveMQConnection) _factory.createConnection(); 
... 
connection.addTransportListener(new ConnectionStateMonitor()); 

재밌게!

+0

또한 연결 URI "failover : tcp : // host : port"에서 장애 조치를 사용할 수도 있습니다. 소켓 예외가 있으면 자동으로 다시 연결하도록 클라이언트에 알려줍니다. http://activemq.apache.org/how-can-i-support-auto-reconnection.html –