Search in sources :

Example 1 with JMXConnection

use of com.googlecode.jmxtrans.connections.JMXConnection in project jmxtrans by jmxtrans.

the class Server method execute.

public Iterable<Result> execute(Query query) throws Exception {
    JMXConnection jmxConnection = pool.borrowObject(this);
    try {
        ImmutableList.Builder<Result> results = ImmutableList.builder();
        MBeanServerConnection connection = jmxConnection.getMBeanServerConnection();
        for (ObjectName queryName : query.queryNames(connection)) {
            results.addAll(query.fetchResults(connection, queryName));
        }
        pool.returnObject(this, jmxConnection);
        return results.build();
    } catch (Exception e) {
        // since we will invalidate the connection in the pool, prevent connection leaks
        try {
            jmxConnection.close();
        } catch (IOException | RuntimeException re) {
            // drop these, we don't really know what caused the original exception.
            logger.warn("An error occurred trying to close a JMX Connection during error handling.", re);
        }
        pool.invalidateObject(this, jmxConnection);
        throw e;
    }
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) ImmutableList(com.google.common.collect.ImmutableList) MBeanServerConnection(javax.management.MBeanServerConnection) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ObjectName(javax.management.ObjectName)

Example 2 with JMXConnection

use of com.googlecode.jmxtrans.connections.JMXConnection in project jmxtrans by jmxtrans.

the class ServerTests method testConnectionRepoolingSkippedOnError_andConnectionIsClosed.

@Test
public void testConnectionRepoolingSkippedOnError_andConnectionIsClosed() throws Exception {
    @SuppressWarnings("unchecked") GenericKeyedObjectPool<JmxConnectionProvider, JMXConnection> pool = mock(GenericKeyedObjectPool.class);
    Server server = Server.builder().setHost("host.example.net").setPort("4321").setLocal(true).setPool(pool).build();
    MBeanServerConnection mBeanConn = mock(MBeanServerConnection.class);
    JMXConnection conn = mock(JMXConnection.class);
    when(conn.getMBeanServerConnection()).thenReturn(mBeanConn);
    when(pool.borrowObject(server)).thenReturn(conn);
    Query query = mock(Query.class);
    IOException e = mock(IOException.class);
    when(query.queryNames(mBeanConn)).thenThrow(e);
    try {
        server.execute(query);
        fail("No exception got throws");
    } catch (IOException e2) {
        if (e != e2) {
            fail("Wrong exception thrown (" + e + " instead of mock");
        }
    }
    verify(conn).close();
    verify(pool, never()).returnObject(server, conn);
    InOrder orderVerifier = inOrder(pool);
    orderVerifier.verify(pool).borrowObject(server);
    orderVerifier.verify(pool).invalidateObject(server, conn);
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) InOrder(org.mockito.InOrder) JmxConnectionProvider(com.googlecode.jmxtrans.connections.JmxConnectionProvider) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 3 with JMXConnection

use of com.googlecode.jmxtrans.connections.JMXConnection in project jmxtrans by jmxtrans.

the class JMXConnectionTest method serverConnectionIsExposed.

@Test
public void serverConnectionIsExposed() {
    MBeanServerConnection mBeanServerConnection = mock(MBeanServerConnection.class);
    JMXConnection jmxConnection = new JMXConnection(null, mBeanServerConnection);
    assertThat(jmxConnection.getMBeanServerConnection()).isSameAs(mBeanServerConnection);
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 4 with JMXConnection

use of com.googlecode.jmxtrans.connections.JMXConnection in project jmxtrans by jmxtrans.

the class JMXConnectionTest method connectorIsClosed.

@Test
public void connectorIsClosed() throws IOException {
    MBeanServerConnection mBeanServerConnection = mock(MBeanServerConnection.class);
    JMXConnector jmxConnector = mock(JMXConnector.class);
    JMXConnection jmxConnection = new JMXConnection(jmxConnector, mBeanServerConnection);
    jmxConnection.close();
    verify(jmxConnector).close();
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 5 with JMXConnection

use of com.googlecode.jmxtrans.connections.JMXConnection in project jmxtrans by jmxtrans.

the class ServerTests method testConnectionRepoolingOk.

@Test
public void testConnectionRepoolingOk() throws Exception {
    @SuppressWarnings("unchecked") GenericKeyedObjectPool<JmxConnectionProvider, JMXConnection> pool = mock(GenericKeyedObjectPool.class);
    Server server = Server.builder().setHost("host.example.net").setPort("4321").setLocal(true).setPool(pool).build();
    MBeanServerConnection mBeanConn = mock(MBeanServerConnection.class);
    JMXConnection conn = mock(JMXConnection.class);
    when(conn.getMBeanServerConnection()).thenReturn(mBeanConn);
    when(pool.borrowObject(server)).thenReturn(conn);
    Query query = mock(Query.class);
    Iterable<ObjectName> objectNames = Lists.emptyList();
    when(query.queryNames(mBeanConn)).thenReturn(objectNames);
    server.execute(query);
    verify(pool, never()).invalidateObject(server, conn);
    InOrder orderVerifier = inOrder(pool);
    orderVerifier.verify(pool).borrowObject(server);
    orderVerifier.verify(pool).returnObject(server, conn);
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) InOrder(org.mockito.InOrder) JmxConnectionProvider(com.googlecode.jmxtrans.connections.JmxConnectionProvider) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

JMXConnection (com.googlecode.jmxtrans.connections.JMXConnection)7 MBeanServerConnection (javax.management.MBeanServerConnection)7 Test (org.junit.Test)6 JmxConnectionProvider (com.googlecode.jmxtrans.connections.JmxConnectionProvider)3 IOException (java.io.IOException)3 InOrder (org.mockito.InOrder)3 ObjectName (javax.management.ObjectName)2 ImmutableList (com.google.common.collect.ImmutableList)1 UnknownHostException (java.net.UnknownHostException)1 JMXConnector (javax.management.remote.JMXConnector)1