sivaPR
sivaPR

Reputation: 41

How to get number of used/opened connections in a MQ Connection factory configured in WebSphere Application Server

I have a java application running on WebSphere 8.5.5.12 server. I connect to other applications via MQ. I have faced a performance issue with the application and found that whenever the MQ reply is getting timeout, the Queue connection was not closed properly. I have fixed the issue. I am planning to increase the Maximum connection for the particular Queue Connection factory and i want to get the number of connection used/opened in the Queue Connection Factory via code, so that i can increase the maximum connections accordingly based on the traffic/volume. Any leads would be much helpful.

Upvotes: 0

Views: 1466

Answers (2)

James M. Stephens
James M. Stephens

Reputation: 11

For your second part of your question, how do you change the maximum connections based on load.

I have a some sample code using a datasource that may help to answer you question. Where I using name=built-in-derby-datasource, you can change the name to your Queue Connection factory name. If you need the lookup, change this jndi name jdbc/built-in-derby-datasource to your Queue Connection factory jndi name.

The code will get the admin client giving you access to queryMBeans. After you have the mbean, you can change maximum connections dynamically while the server is running.

@SuppressWarnings("unchecked")
public void AdminClientExample() 
{
    Object adminClient = null;

    // Need to set the properties, type, host and port, defaults likely will work for most
    Properties acProps = new Properties();
    acProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
    acProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
    acProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");

    // Set if security is enabled
    //acProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
    //acProps.setProperty(AdminClient.USERNAME, "userid");
    //acProps.setProperty(AdminClient.PASSWORD, "userid password");

    try 
    {
        adminClient = AdminClientFactory.createAdminClient(acProps);

    } 
    catch (Exception e)  
    {
        e.printStackTrace();
    }

    ObjectInstance obi = null;
    ObjectName obn = null;
    Set<ObjectInstance> s = null;
    try {
        // The two types to use are J2CConnectionFactory and DataSource if searching through a list of mbeans of that type.
        // type=J2CConnectionFactory
        // type=DataSource
        // obn = new ObjectName("WebSphere:type=DataSource,*");
        // s1 =((AdminClient)adminClient).queryMBeans(obn, null);   // search through s1

        // You can provide the name like this, 
        obn = new ObjectName("WebSphere:name=built-in-derby-datasource,*");
        s =((AdminClient)adminClient).queryMBeans(obn, null);
        // s should contain WebSphere:name=built-in-derby-datasource,process=server1,platform=dynamicproxy,node=DefaultNode01,JDBCProvider=Derby JDBC Provider (XA),diagnosticProvider=true,j2eeType=JDBCDataSource,J2EEServer=server1,Server=server1,version=9.0.0.11,type=DataSource,mbeanIdentifier=cells/DefaultCell01/resources.xml#DataSource_9007001,JDBCResource=Derby JDBC Provider (XA),cell=DefaultCell01,spec=1.0
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (s == null) {
        System.out.println("Did not find MBeans querying for object name " + obn.toString());
        return;
    } else {
        obi = s.iterator().next();  
    }

    // Normally the application using the connection pool will have
    // already done the lookup which creates the objects
    // required to change maxConnections.  This lookup is only for
    // this example.
    InitialContext ctx;
    try {  
        ctx = new InitialContext();
        ctx.lookup("jdbc/built-in-derby-datasource");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents
    Object [] parms =  null;
    String [] parmsTypes = null;
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
    } catch (Exception e) {
        e.printStackTrace(); 
    }

    // get maxConnections
    try {
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // change the maxConnections to 11,
    try {
        Integer it = new Integer(11);
        Attribute at = new Attribute("maxConnections", it);
        ((AdminClient)adminClient).setAttribute(obi.getObjectName(), at );
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents, maxConnection now should be 11.
    // or you can use the get maxConnection to check
    // the changed value.
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
        // or
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

Upvotes: 1

Morag Hughson
Morag Hughson

Reputation: 7515

To learn the number of connections used and the number of queues opened by an application, you can use the MQSC DISPLAY CONN command like this:-

DISPLAY CONN(*) TYPE(ALL) ALL WHERE(OBJNAME EQ reply-q-name)

This will show you all the connections and all the open handles.

You can also discover exactly the same data using a programatic interface called PCF commands, although given how many excellent MQ admin tools there are out there, I'm not sure why you would need to do this "via code" as you put it?

Upvotes: 1

Related Questions