可以透過該API管理QMGR
MQAgent.java---
package cgw.mq.admin;
import cgw.mq.bean.MQManageBean;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.pcf.PCFMessageAgent;
public class MQAgent {
private PCFMessageAgent agent;
public MQAgent(MQManageBean bean ,int ccsid){
//MQEnvironment.CCSID = ccsid;
try {
PCFMessageAgent agent = new PCFMessageAgent(bean.getMqm_ip(),bean.getMqm_port(),bean.getAdmin_channel());
} catch (MQException e) {
e.printStackTrace();
}
agent.setCharacterSet(ccsid);
}
public PCFMessageAgent getAgent() {
return agent;
}
public void close() throws MQException{
agent.disconnect();
}
}
MQManageBean.java---
package cgw.mq.bean;
public class MQManageBean {
private String mqm_ip;
private int mqm_port;
private String admin_channel;
public String getAdmin_channel() {
return admin_channel;
}
public void setAdmin_channel(String admin_channel) {
this.admin_channel = admin_channel;
}
public String getMqm_ip() {
return mqm_ip;
}
public void setMqm_ip(String mqm_ip) {
this.mqm_ip = mqm_ip;
}
public int getMqm_port() {
return mqm_port;
}
public void setMqm_port(int mqm_port) {
this.mqm_port = mqm_port;
}
}
MQChannel.java---
package cgw.mq.admin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cgw.mq.bean.MQChannelBean;
import cgw.mq.bean.MQManageBean;
import com.ibm.mq.MQException;
import com.ibm.mq.pcf.CMQC;
import com.ibm.mq.pcf.CMQCFC;
import com.ibm.mq.pcf.CMQXC;
import com.ibm.mq.pcf.PCFException;
import com.ibm.mq.pcf.PCFMessage;
import com.ibm.mq.pcf.PCFMessageAgent;
public class MQChannel {
private PCFMessageAgent agent;
private int[] attrs = { CMQCFC.MQCACH_CHANNEL_NAME,
CMQCFC.MQCACH_CONNECTION_NAME, CMQCFC.MQIACH_MSGS,
CMQCFC.MQCACH_LAST_MSG_DATE, CMQCFC.MQCACH_LAST_MSG_TIME,
CMQCFC.MQIACH_CHANNEL_STATUS, CMQCFC.MQIACH_CHANNEL_TYPE };
public static void main(String args[]) throws MQException {
MQManageBean bean = new MQManageBean();
bean.setMqm_ip("192.168.1.2");
bean.setMqm_port(1416);
bean.setAdmin_channel("SYSTEM.DEF.SVRCONN");
MQAgent agent = new MQAgent(bean, 950);
MQChannel mqc = new MQChannel();
// PCFMessageAgent agent = new PCFMessageAgent("testPcf");
mqc.setAgent(agent.getAgent());
try {
//mqc.startChannelListener();
MQChannelBean channelBean = new MQChannelBean();
channelBean.setChannelName("go");
channelBean.setChannelType(CMQXC.MQCHT_SVRCONN);
mqc.createChannel(channelBean);
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List chkChannel(String targetChannel) {
ArrayList beanList = new ArrayList();
try {
PCFMessage request = new PCFMessage(
CMQCFC.MQCMD_INQUIRE_CHANNEL_STATUS);
request.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, targetChannel);
request.addParameter(CMQCFC.MQIACH_CHANNEL_INSTANCE_TYPE,
CMQC.MQOT_CURRENT_CHANNEL);
request.addParameter(CMQCFC.MQIACH_CHANNEL_INSTANCE_ATTRS, attrs);
PCFMessage[] pcf_resp = getAgent().send(request);
for (int i = 0; i < pcf_resp.length; i++){
MQChannelBean bean = new MQChannelBean();
bean.setChannelName(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_CHANNEL_NAME));
bean.setChannelType(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_CHANNEL_TYPE));
bean
.setConnectionName(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_CONNECTION_NAME));
bean.setMsgCount(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_MSGS));
bean.setLastMsgDate(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_LAST_MSG_DATE));
bean.setLastMsgTime(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_LAST_MSG_TIME));
bean.setStatus(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_CHANNEL_STATUS));
beanList.add(bean);
}
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return beanList;
}
public void startChannelListener() throws PCFException, MQException, IOException{
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_START_CHANNEL_LISTENER);
getAgent().send(channel_msg);
}
public void stopChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_STOP_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQCACH_CONNECTION_NAME, bean
.getConnectionName());
getAgent().send(channel_msg);
}
public void startChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_START_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQCACH_CONNECTION_NAME, bean
.getConnectionName());
getAgent().send(channel_msg);
}
public void createChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_CREATE_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQIACH_CHANNEL_TYPE, bean
.getChannelType());
getAgent().send(channel_msg);
}
public PCFMessageAgent getAgent() {
return agent;
}
public void setAgent(PCFMessageAgent agent) {
this.agent = agent;
}
}
MQQueue.java---
package cgw.mq.admin;
import java.io.IOException;
import cgw.mq.bean.MQManageBean;
import com.ibm.mq.MQException;
import com.ibm.mq.pcf.CMQC;
import com.ibm.mq.pcf.CMQCFC;
import com.ibm.mq.pcf.PCFException;
import com.ibm.mq.pcf.PCFMessage;
import com.ibm.mq.pcf.PCFMessageAgent;
public class MQQueue {
private String targetQueue;
private int depth;
private PCFMessage pcf_req;
private PCFMessageAgent agent;
private int[] attrs = { CMQC.MQCA_Q_NAME, CMQC.MQIA_CURRENT_Q_DEPTH };
public static void main(String[] arg){
MQManageBean bean = new MQManageBean();
bean.setMqm_ip("192.168.1.1");
bean.setMqm_port(1414);
bean.setAdmin_channel("SYSTEM.DEF.SVRCONN");
MQAgent agent = new MQAgent(bean,950);
MQQueue mqq = new MQQueue();
mqq.setTargetQueue("TEST");
mqq.setAgent(agent.getAgent());
mqq.init();
/*
try {
//mqq.createQueue();
//mqq.removeQueue();
//mqq.clearQueue();
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
mqq.chkQueueDepth();
System.out.println(mqq.getDepth());
}
public void init(){
pcf_req = new PCFMessage(CMQCFC.MQCMD_INQUIRE_Q);
pcf_req.addParameter(CMQC.MQCA_Q_NAME, getTargetQueue());
pcf_req.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);
pcf_req.addParameter(CMQCFC.MQIACF_Q_ATTRS, attrs);
}
public void chkQueueDepth(){
try {
PCFMessage[] pcf_resp = getAgent().send(getPcf_req());
for(int i = 0;i
String tmp = pcf_resp[i].getStringParameterValue(CMQC.MQCA_Q_NAME).trim();
if(getTargetQueue().equals(tmp)){
setDepth(pcf_resp[i].getIntParameterValue(CMQC.MQIA_CURRENT_Q_DEPTH));
}else{
setDepth(-1);
}
}
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createQueue() throws PCFException, MQException, IOException{
PCFMessage queue_msg = new PCFMessage(CMQCFC.MQCMD_CREATE_Q);
queue_msg.addParameter(CMQC.MQCA_Q_NAME, getTargetQueue());
queue_msg.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);
getAgent().send(queue_msg);
}
public void removeQueue() throws PCFException, MQException, IOException{
PCFMessage queue_msg = new PCFMessage(CMQCFC.MQCMD_DELETE_Q);
queue_msg.addParameter(CMQC.MQCA_Q_NAME, getTargetQueue());
queue_msg.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);
getAgent().send(queue_msg);
}
public void clearQueue() throws PCFException, MQException, IOException{
PCFMessage queue_msg = new PCFMessage(CMQCFC.MQCMD_CLEAR_Q);
queue_msg.addParameter(CMQC.MQCA_Q_NAME, getTargetQueue());
//queue_msg.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);
getAgent().send(queue_msg);
}
public String getTargetQueue() {
return targetQueue;
}
public void setTargetQueue(String targetQueue) {
this.targetQueue = targetQueue;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public PCFMessage getPcf_req() {
return pcf_req;
}
public void setPcf_req(PCFMessage pcf_req) {
this.pcf_req = pcf_req;
}
public PCFMessageAgent getAgent() {
return agent;
}
public void setAgent(PCFMessageAgent agent) {
this.agent = agent;
}
}
package cgw.mq.admin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cgw.mq.bean.MQChannelBean;
import cgw.mq.bean.MQManageBean;
import com.ibm.mq.MQException;
import com.ibm.mq.pcf.CMQC;
import com.ibm.mq.pcf.CMQCFC;
import com.ibm.mq.pcf.CMQXC;
import com.ibm.mq.pcf.PCFException;
import com.ibm.mq.pcf.PCFMessage;
import com.ibm.mq.pcf.PCFMessageAgent;
public class MQChannel {
private PCFMessageAgent agent;
private int[] attrs = { CMQCFC.MQCACH_CHANNEL_NAME,
CMQCFC.MQCACH_CONNECTION_NAME, CMQCFC.MQIACH_MSGS,
CMQCFC.MQCACH_LAST_MSG_DATE, CMQCFC.MQCACH_LAST_MSG_TIME,
CMQCFC.MQIACH_CHANNEL_STATUS, CMQCFC.MQIACH_CHANNEL_TYPE };
public static void main(String args[]) throws MQException {
MQManageBean bean = new MQManageBean();
bean.setMqm_ip("192.168.1.2");
bean.setMqm_port(1416);
bean.setAdmin_channel("SYSTEM.DEF.SVRCONN");
MQAgent agent = new MQAgent(bean, 950);
MQChannel mqc = new MQChannel();
// PCFMessageAgent agent = new PCFMessageAgent("testPcf");
mqc.setAgent(agent.getAgent());
try {
//mqc.startChannelListener();
MQChannelBean channelBean = new MQChannelBean();
channelBean.setChannelName("go");
channelBean.setChannelType(CMQXC.MQCHT_SVRCONN);
mqc.createChannel(channelBean);
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List chkChannel(String targetChannel) {
ArrayList beanList = new ArrayList();
try {
PCFMessage request = new PCFMessage(
CMQCFC.MQCMD_INQUIRE_CHANNEL_STATUS);
request.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, targetChannel);
request.addParameter(CMQCFC.MQIACH_CHANNEL_INSTANCE_TYPE,
CMQC.MQOT_CURRENT_CHANNEL);
request.addParameter(CMQCFC.MQIACH_CHANNEL_INSTANCE_ATTRS, attrs);
PCFMessage[] pcf_resp = getAgent().send(request);
for (int i = 0; i <>
MQChannelBean bean = new MQChannelBean();
bean.setChannelName(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_CHANNEL_NAME));
bean.setChannelType(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_CHANNEL_TYPE));
bean
.setConnectionName(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_CONNECTION_NAME));
bean.setMsgCount(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_MSGS));
bean.setLastMsgDate(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_LAST_MSG_DATE));
bean.setLastMsgTime(pcf_resp[i]
.getStringParameterValue(CMQCFC.MQCACH_LAST_MSG_TIME));
bean.setStatus(pcf_resp[i]
.getIntParameterValue(CMQCFC.MQIACH_CHANNEL_STATUS));
beanList.add(bean);
}
} catch (PCFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return beanList;
}
public void startChannelListener() throws PCFException, MQException, IOException{
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_START_CHANNEL_LISTENER);
getAgent().send(channel_msg);
}
public void stopChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_STOP_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQCACH_CONNECTION_NAME, bean
.getConnectionName());
getAgent().send(channel_msg);
}
public void startChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_START_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQCACH_CONNECTION_NAME, bean
.getConnectionName());
getAgent().send(channel_msg);
}
public void createChannel(MQChannelBean bean) throws PCFException,
MQException, IOException {
PCFMessage channel_msg = new PCFMessage(CMQCFC.MQCMD_CREATE_CHANNEL);
channel_msg.addParameter(CMQCFC.MQCACH_CHANNEL_NAME, bean
.getChannelName());
channel_msg.addParameter(CMQCFC.MQIACH_CHANNEL_TYPE, bean
.getChannelType());
getAgent().send(channel_msg);
}
public PCFMessageAgent getAgent() {
return agent;
}
public void setAgent(PCFMessageAgent agent) {
this.agent = agent;
}
}
留言
張貼留言