Primero, presentemos brevemente websphere mq y su instalación y uso.
websphere mq: se utiliza para transmitir información con funcionalidad multiplataforma.
1 Instale websphere mq y comience
2 websphere mq crea un administrador de colas (como: MQSI_SAMPLE_QM)
3. Cree el tipo de cola y seleccione el tipo Local (como lq)
4. Establezca el tipo de canales y seleccione Conexión del servidor (como BridgeChannel)
A continuación, veamos el código de ejemplo:
MQFileReceiver.javapackage com.mq.dpca.file; importar java.io.File; importar java.io.FileOutputStream; importar com.ibm.mq.MQEnvironment; MQGetMessageOptions;importar com.ibm.mq.MQMessage;importar com.ibm.mq.MQQueue;importar com.ibm.mq.MQQueueManager;importar com.ibm.mq.constants.MQConstants;importar com.mq.dpca.msg.MQConfig;importar com.mq.dpca.util.ReadCmdLine; importar com.mq.dpca.util.RenameUtil /**; * * Función de archivo de recepción del grupo MQ * Sondeo activo */public class MQFileReceiver { private MQQueueManager qmgr; // Conectarse al administrador de colas private MQQueue inQueue // Cola de transmisión private String queueName = "" // Nombre de la cola private String host = ""; // puerto int privado = 1414; // número de puerto del oyente cadena privada canal = "" // nombre del canal cadena privada qmgrName = ""; // Administrador de colas private MQMessage inMsg; // Crear búfer de mensajes private MQGetMessageOptions gmo; // Establecer opciones de adquisición de mensajes private static String fileName = null; ccsid = 0; cadena estática privada file_dir = null /** * Entrada de programa* * @param args */ public static void main(String args[]) { MQFileReceiver mfs = new MQFileReceiver(); //Inicializa la conexión mfs.initproperty(); //Recibir archivos mfs.runGoupReceiver(); //Obtener el nombre del script de shell // String shellname = MQConfig.getValueByKey(fileName); if( shellname!=null&&!"".equals(shellname)){// //Llamando al shell// ReadCmdLine.callShell(shellname);// }else{// System.out.println("no tiene nombre de shell, solo recibe archivos.");// } } public void runGoupReceiver() { try { init(); (); qmgr.commit(); System.out.println("/n Mensajes recibidos exitosamente "); mqe) { mqe.printStackTrace(); intente { System.out.println("/n Retirar transacción "); qmgr.backout(); ); System.exit(2); } } catch (Excepción e) { e.printStackTrace(); Inicializar información de conexión del servidor * * @throws Exception */ private void init() throws Exception { /* Establecer propiedades de MQEnvironment para conexiones de cliente */ MQEnvironment.hostname = host; al administrador de colas*/ qmgr = new MQQueueManager(qmgrName); Establezca la opción de apertura de cola para entrada */ int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING /* Abra la cola para entrada */ inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null); * Aceptar La función principal del archivo * * @throws Excepción */ public void getGroupMessages() { /* Establecer opciones de obtención de mensajes */ gmo = new MQGetMessageOptions(); gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING /* Esperar mensajes * /gmo.opciones = gmo.opciones + MQConstants.MQGMO_WAIT /* Establecer límite de tiempo de espera */ gmo.waitInterval = 5000; /* Obtener solo mensajes */ gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE /* Obtener mensajes en orden de edición */ gmo.options = gmo.options + MQConstants.MQGMO_LOGICAL_ORDER; gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID; /* Crear búfer de mensajes*/ inMsg = new MQMessage(); try { FileOutputStream fos = null /* Procesar mensaje de grupo*/ while (true) { try { inQueue.get(inMsg, gmo; ); if (fos == null) { intentar { nombre de archivo = inMsg.getStringProperty("fileName"); String fileName_full = null; fileName_full = file_dir + RenameUtil.rename(fileName); fos = new FileOutputStream(new File(fileName_full)); = nuevo byte[msgLength]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); /* Compruebe si es el último identificador de mensaje*/ char x = gmo.groupStatus if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out; .println ("Último mensaje del grupo"); break } inMsg.clearMessage(); (Excepción e) { System.out .println("Reciba el mensaje sin propiedad, ¡no haga nada!"); inMsg.clearMessage(); } } else { int msgLength = inMsg.getMessageLength(); [msgLength]; enMsg.readFully(búfer); fos.write(búfer, 0, msgLength); Compruebe si es el último identificador de mensaje*/ char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Último mensaje en grupo" } inMsg.clearMessage(); } } catch (Excepción e) { char x = gmo.groupStatus si (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Último mensaje en grupo"); } break; } } if (fos! = null) fos.close() } catch (Excepción e) { System.out.println(; e.getMessage()); } } public void initproperty() { configuración MQConfig = nuevo MQConfig().getInstance(); if (config.getMQ_MANAGER() != null) { qmgrName = config.getMQ_MANAGER(); queueName = config.getMQ_QUEUE_NAME() canal = config.getMQ_CHANNEL(); ); puerto = Integer.valueOf(config.getMQ_PROT()); ccsid = Integer.valueOf(config.getMQ_CCSID());