当前位置:编程学习 > 网站相关 >>

MQ出现2059错误如何快速断开

是这样的,我用JAVA写了一个连接MQ的接口,在使用过程中发现,业务功能在调用此接口后,如果MQ连接出现问题
会出现比较长时间的等待,由于数据处理是放在一个事物中的,这样就会导致功能页面长时间静止。
请问如何解决此问题。
public String sentData(){


    String qManager = manger; //QueueManager name
    String qName = qname;//Queue Name
    MQQueueManager qMgr = null;
    MQQueue queue = null ;
            try {
              //configure connection parameters
           MQEnvironment.hostname=hostname;//MQ Server name or IP
              //MQEnvironment.port=1414;//listenr port
           if(log.isDebugEnabled())
              log.debug("Connecting to queue hostname: "+hostname);
              MQEnvironment.channel=channel;//Server-Connection Channel
              if(log.isDebugEnabled())
              log.debug("Connecting to queue channel: "+channel);
              MQEnvironment.CCSID =ccsid;
              if(log.isDebugEnabled())
              log.debug("Connecting to queue ccsid: "+ccsid);
              // Create a connection to the QueueManager
              if(log.isDebugEnabled())
              log.debug("Connecting to queue MQQueueManager: "+qManager);
              
              qMgr = new MQQueueManager(qManager);
            // Set up the options on the queue we wish to open 
            //int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
          int openOptions = MQC.MQOO_OUTPUT|MQC.MQOO_BIND_ON_OPEN|MQC.MQOO_FAIL_IF_QUIESCING ;//-----------------
            // Now specify the queue that we wish to open and the open options
          if(log.isDebugEnabled())
              log.debug("Accessing queue: "+qName);
            
          queue = qMgr.accessQueue(qName, openOptions);
              // Define a simple WebSphere MQ Message ... 
              MQMessage msg = new MQMessage();
              // ... and write some text in UTF8 format
              
              //File file = new File("d:\\sd.000000129497.00000001.13500201.data");


//                     for(int m=0;m<187;m++){
//                      System.out.print(""+data[m]);
//                     
//                     }

              //data=getBytesFromFile(file);
                  //FileOutputStream fos = new FileOutputStream(new File("D:/data.txt"));
                     //写入文件
                  //fos.write(data);


//                     for(int m=0;m<datalength;m++){
//                      System.out.print(" "+data[m]);
//                     
//                     }

 
              
              String head="";
               for(int m=recvcode.length();m<8;m++){
               head=head+"0";
               }
               recvcode=head+recvcode;
             
               
              recv=recvcode.getBytes();
             
              msg.correlationId=recv;
              
              
              
              //Specify the default put message options
              MQPutMessageOptions pmo = new MQPutMessageOptions();
              int totallen;
              int totalduan;
//               int messlen = 8*1024;
              totallen = 0;
             totalduan = 0;
//            2011-1-13 玄加林调整--解决数据量大时发送失败的问题
//              while((totallen + messlen) < datalength)////8888888
//             {
//              pmo.options = 32770;//---------------------------
//              msg.messageFlags = 2;//-----------------------
//              msg.write(data,totallen,messlen);//8888888888888
//              queue.put(msg, pmo);
//              totallen += messlen;
//              totalduan ++;
//             }
           msg.messageFlags = 4;
           msg.write(data,totallen,datalength-totallen);
           queue.put(msg, pmo);
           totalduan++;
              // Put the message to the queue
          if(log.isDebugEnabled())
              log.debug("Sending a message...");
              return "发送成功";

          }
          catch (MQException ex) {
           //出现异常直接关闭连接
           try {
           if(qMgr != null){
      qMgr.disconnect();
           }
           } catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
           }
           if(log.isDebugEnabled())
              log.debug("A WebSphere MQ Error occured : Completion Code "
                      + ex.completionCode + " Reason Code " + ex.reasonCode);
              return "发送失败";
          }
          catch (java.io.IOException ex) {
           
           if(log.isDebugEnabled())
              log.debug("An IOException occured whilst writing to the message buffer: "
                      + ex);
              return "发送失败";
          }
          finally{ //根据玄加林的要求,增加关闭mq连接的操作 2010-12-21 ---linpeng
          try {
queue.close();
} catch (MQException e) {
// TODO Auto-generated catch block
try {
qMgr.disconnect();
} catch (MQException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
          try {
qMgr.disconnect();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
          }

} --------------------编程问答-------------------- 已解决,捕捉异常后
switch   (ex.reasonCode) 
                      { 
                             //连接断开 
                             case   2009: 
                              //队列管理者无效或未知 
                              case   2058: 
                              //队列管理者无效 
                              case   2059: 
                             //意外的错误发生 
                              case   2195: 
                              //服务器正在启动 
                              case   2161: 
                             //服务器正在关闭 
                              case   2162:        
                              //无权访问服务器队列管理者 
                             case   2035:       
                              //其他异常 
                              default: 
                                      
                      }   
根据异常类型马上进行处理,也可捕捉异常后马上关闭连接
try {
           if(qMgr != null){
      qMgr.disconnect();
           }
           } catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
           }  --------------------编程问答-------------------- 代码整理下。。没法看。 --------------------编程问答-------------------- 有定义超时时间吗? --------------------编程问答-------------------- 有定义超时时间吗? --------------------编程问答-------------------- 代码整理下。。没法看。 --------------------编程问答-------------------- 有定义超时时间吗? --------------------编程问答-------------------- 加入错误返回代码 --------------------编程问答-------------------- 加入错误代码
补充:企业软件 ,  WebSphere
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,