Primeiro, vamos apresentar brevemente o websphere mq e sua instalação e uso.
websphere mq: usado para transmitir informações com funcionalidade de plataforma cruzada.
1 Instale o websphere mq e inicie
2 websphere mq cria gerenciador de filas (como: MQSI_SAMPLE_QM)
3. Crie o tipo de fila e selecione o tipo local (como lq)
4. Estabeleça o tipo de canal e selecione Conexão do Servidor (como BridgeChannel)
A seguir, vejamos o código de exemplo:
MQFileReceiver.javapackage com.mq.dpca.file; importar java.io.File; importar java.io.FileOutputStream; 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; * * Função de arquivo de recebimento do grupo MQ * Sondagem ativa */public class MQFileReceiver { private MQQueueManager qmgr; // Conecta-se ao gerenciador de filas private MQQueue inQueue; // Fila de transmissão private String queueName = ""; ""; // private int port = 1414; // Número da porta do ouvinte private String channel = ""; qmgrName = ""; // Gerenciador de filas private MQMessage inMsg; // Cria buffer de mensagens private MQGetMessageOptions gmo; // Define opções de aquisição de mensagens private static String fileName = null; ccsid = 0; private static String file_dir = null /** * Entrada do programa* * @param args */ public static void main(String args[]) { MQFileReceiver mfs = new MQFileReceiver(); //Inicializar a conexão mfs.initproperty(); //Receber arquivos mfs.runGoupReceiver();// String shellname = MQConfig.getValueByKey(fileName);// if(shellname!=null&&!"".equals(shellname)){// //Chamando shell// ReadCmdLine.callShell(shellname);// }else{// System.out.println("não tem nome de shell, recebe apenas arquivos.");// } } public void runGoupReceiver() { try { init(); (); qmgr.commit(); System.out.println("/n Mensagens recebidas com sucesso "); mqe) { mqe.printStackTrace(); try { System.out.println("/n Recuperando transação "); ); System.exit(2); Inicializar informações de conexão do servidor * * @throws Exception */ private void init() throws Exception { /* Configurar propriedades do MQEnvironment para conexões do cliente */ MQEnvironment.hostname = host; para o gerenciador de filas*/ qmgr = new MQQueueManager(qmgrName); Configure a opção de abertura de fila para entrada */ int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING /* Abra a fila para entrada */ inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null); * Aceite a função principal do arquivo * * @throws Exceção */ public void getGroupMessages() { /* Definir opções de obtenção de mensagens */ gmo = new MQGetMessageOptions(); /gmo.options=gmo.options + MQConstants.MQGMO_WAIT; /* Definir limite de tempo de espera */ gmo.waitInterval = 5000; /* Obter mensagens apenas */ gmo.options = gmo.options + MQGMO_ALL_MSGS_AVAILABLE; gmo.options + MQConstants.MQGMO_LOGICAL_ORDER; gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID; /* Criar buffer de mensagem*/ inMsg = new MQMessage(); try { FileOutputStream fos = null; ); if (fos == nulo) { tente { nomeDoArquivo = inMsg.getStringProperty("fileName"); String fileName_full = null; fileName_full = file_dir + RenameUtil.rename(fileName) = new FileOutputStream(new File(fileName_full)); = novo byte[comprimentomsg]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); /* Verifica se é o último identificador de mensagem*/ char x = gmo.groupStatus if (x == MQGS_LAST_MSG_IN_GROUP) { System.out .println ("Última mensagem no grupo"); } inMsg.clearMessage(); (Exceção e) { System.out .println("Receba a mensagem sem propriedade, não faça nada!"); inMsg.clearMessage() } } else { int msgLength = inMsg.getMessageLength(); [msgLength];inMsg.readFully(buffer); Verifique se é o identificador da última mensagem*/ char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Última mensagem no grupo"); } } catch (Exceção e) { char x = gmo.groupStatus if (x ==; MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Última mensagem no grupo"); e.getMessage()); } } public void initproperty() { configuração MQConfig = novo MQConfig().getInstance(); if (config.getMQ_MANAGER() != null) { qmgrName = config.getMQ_MANAGER(); ); porta = Integer.valueOf(config.getMQ_PROT());