Wednesday, 30 April 2014

Implementing Log4J on PCF related Java class

Step1:
Create a java project in Eclipse with name  GETCLUSQMGR.

Step2:
By default  we have a "src" folder  under  this folder  create a package by name com.logger.test and place the java classes with PCF,Logger  business logic.

Step3:
Create a source folder by name "resource" and add log4j2.xml file under it.

Step4:
Now the folder structure of the java project  will look like below


Step5:
Add the below code to ClusQmgr.java

package com.logger.test;
import java.io.IOException;
import org.apache.logging.log4j.Logger;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.pcf.PCFException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
import com.ibm.mq.headers.MQDataException;


public class ClusQmgr
{     
static final Logger    logger      = CodeLogger.getLogger(ClusQmgr.class.getName());

public static void main (String [] args) throws MQDataException,PCFException, MQDataException,IOException
    {
              logger.debug("PCFMessageAgentCreation Start");
               PCFMessageAgent agent = new PCFMessageAgent("EB310000");

               logger.debug("Inquiring Cluster Queue Managers");
                PCFMessage pcfCmd = new PCFMessage(CMQCFC.MQCMD_INQUIRE_CLUSTER_Q_MGR);

                  pcfCmd.addParameter(CMQC.MQCA_CLUSTER_Q_MGR_NAME,"*");
      
                  logger.debug("Cluster Queue Managers Details");
                PCFMessage[] pcfResponse =agent.send(pcfCmd);

        System.out.println("**QueueManager**Host**Port**");
         String ClustQmgr=" ";
         String[] ConDetails;
         String Hostname=" ";
         String Port=" ";

 for (PCFMessage pcfMessage : pcfResponse)
{
     ClustQmgr=pcfMessage.getParameterValue(CMQC.MQCA_CLUSTER_Q_MGR_NAME).toString().trim();
      ConDetails=(pcfMessage.getParameterValue(MQConstants.MQCACH_CONNECTION_NAME).toString().trim()).split("\\(",2);
     Hostname=ConDetails[0];
     Port=ConDetails[1].substring(0,ConDetails[1].length()-1 );
      System.out.println("*****"+ClustQmgr+"*******"+Hostname+"*********"+Port+"**********");
      logger.debug("Values"+"**"+ClustQmgr+"**"+Hostname+"**"+Port+"**");
                    
       }
    logger.debug("End of PCF Details");

}
}

Step6:
Add the below code to CodeLogger.java

package com.logger.test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

public final class CodeLogger {
     
private static final String   LOG4J_CONFIG_FILE = "log4j2.xml";
     
static
{
ConfigurationSource source = new ConfigurationSource();
source.setInputStream(CodeLogger.class.getClassLoader().getResourceAsStream(LOG4J_CONFIG_FILE));
Configurator.initialize(CodeLogger.class.getClassLoader(), source);
}
     
public static Logger getLogger(String name)
{
return LogManager.getLogger(name);
}
}

Step7:
Add the below properties  to log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<properties>
<property name="logFileDir">C:\\LOG4J\\</property>
<property name="logFileName">logger</property>
</properties>

<appenders>
             
<RollingFile name="RollingFile" fileName="${logFileDir}${logFileName}.log"
                     filePattern="${logFileDir}$${date:yyyy-MM}/${logFileName}-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n" />

<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>

</RollingFile>

</appenders>
<!-- Logging done on particular package bases "com.logger.test"-->
<loggers>
<logger name="com.logger.test" additivity="false" level="DEBUG">
<AppenderRef ref="RollingFile" />
</logger>
</loggers>

</configuration>

Step8:
Add the below jars to the Java Build Path of the project
log4j-api-2.0-rc1.jar, log4j-core-2.0-rc1.jar

Step9:
OutPut of the logger file will look like below

[2014-04-30 19:58:18,493] [main] DEBUG com.logger.test.ClusQmgr - PCFMessageAgentCreation Start
[2014-04-30 19:58:18,758] [main] DEBUG com.logger.test.ClusQmgr - Inquiring Cluster Queue Managers
[2014-04-30 19:58:18,758] [main] DEBUG com.logger.test.ClusQmgr - Cluster Queue Managers Details
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - Values**EB310000**miracle-PC**1000**
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - Values**EB310001**miracle-PC**1003**
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - End of PCF Details








Tuesday, 15 April 2014

PCF Commands

 The purpose of WebSphere MQ programmable command format (PCF) commands is to allow administration tasks to be programmed into an administration program. In this way you can create queues, process definitions, channels, and namelists, and change queue managers, from a program.


     Each PCF command is a data structure that is embedded in the application data part of a WebSphere MQ message. Each command is sent to the target queue manager using the MQI function MQPUT in the same way as any other message. The command server on the queue manager receiving the message interprets it as a command message and runs the command. To get the replies, the application issues an MQGET call and the reply data is returned in another data structure. The application can then process the reply and act accordingly.

Briefly, these are some of the things the application programmer must specify to create a PCF command message:

Message descriptor

This is a standard IBM MQ message descriptor, in which:
·         Message type ( MsgType ) is MQMT_REQUEST.
·         Message format ( Format ) is MQFMT_ADMIN.
Application data

Contains the PCF message including the PCF header, in which:
·         The PCF message type ( Type ) specifies MQCFT_COMMAND.
·         The command identifier specifies the command, for example, Change Queue (MQCMD_CHANGE_Q).

Escape PCFs are PCF commands that contain MQSC commands within the message text. You can use PCFs to send commands to a remote queue manager.

Sample Java code to get cluster QueueManager Details

import java.io.IOException;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.pcf.PCFException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
import com.ibm.mq.headers.MQDataException;


public class ClusQmgr {
     
public static void main (String [] args) throws MQDataException,PCFException, MQDataException,IOException
    {
//intializes a new PCFMessageAgent with a binding connection to a QueueManager
PCFMessageAgent agent = new PCFMessageAgent("EB310000");

//initializes PCFMessage as a PCF request
PCFMessage pcfCmd = new PCFMessage(CMQCFC.MQCMD_INQUIRE_CLUSTER_Q_MGR);

//add PCF parameters to PCFMessage
 pcfCmd.addParameter(CMQC.MQCA_CLUSTER_Q_MGR_NAME,"*");

//sends a PCF request to connect Queue Manager and return the responses
PCFMessage[] pcfResponse =agent.send(pcfCmd);
              
String ClustQmgr=" ";
String[] ConDetails;
String Hostname=" ";
String Port=" ";

System.out.println("*****QueueManager*******Host*********Port**********");

for (PCFMessage pcfMessage : pcfResponse)
{

//get cluster Queue Manager Name      ClustQmgr=pcfMessage.getParameterValue(CMQC.MQCA_CLUSTER_Q_MGR_NAME).toString().trim();

//get connection Details      ConDetails=(pcfMessage.getParameterValue(MQConstants.MQCACH_CONNECTION_NAME).toString().trim()).split("\\(",2);

//get Host name
Hostname=ConDetails[0];

//get port name
Port=ConDetails[1].substring(0,ConDetails[1].length()-1 );
      System.out.println("*****"+ClustQmgr+"*******"+Hostname+"*********"+Port+"**********");
                 
            }
           
            }