먼저 websphere mq와 설치 및 사용법에 대해 간략히 소개하겠습니다.
websphere mq: 크로스 플랫폼 기능으로 정보를 전송하는 데 사용됩니다.
1 websphere mq를 설치하고 시작합니다.
2 websphere mq는 대기열 관리자(예: MQSI_SAMPLE_QM)를 생성합니다.
3. 대기열 유형을 생성하고 로컬 유형(예: lq)을 선택합니다.
4. 채널 유형을 설정하고 서버 연결(예: BridgeChannel)을 선택합니다.
다음으로 예제 코드를 살펴보겠습니다.
MQFileReceiver.javapackage com.mq.dpca.file; import java.io.FileOutputStream; import com.ibm.mq.MQException; MQGetMessageOptions;import com.ibm.mq.MQMessage;import com.ibm.mq.MQQueue;import com.ibm.mq.MQQueueManager;import com.ibm.mq.constants.MQConstants;import com.mq.dpca.msg.MQConfig;import com.mq.dpca.util.ReadCmdLine; import com.mq.dpca.util.RenameUtil /** * * MQ 그룹 수신 파일 기능 * 활성 폴링 */public class MQFileReceiver { private MQQueueManager qmgr; // 큐 관리자에 연결 private MQQueue inQueue; // 큐 이름 private String 호스트 = ""; // private int port = 1414; // 리스너 포트 번호 private String 채널 = ""; qmgrName = ""; // 큐 관리자 private MQMessage inMsg; // 메시지 버퍼 생성 private MQGetMessageOptions gmo; // 메시지 획득 옵션 설정 private static String fileName = null; ccsid = 0; private static String file_dir = null; /** * 프로그램 항목* * @param args */ public static void main(String args[]) MQFileReceiver mfs = new MQFileReceiver(); //연결 초기화 mfs.initproperty(); //파일 수신 mfs.runGoupReceiver() //쉘 스크립트 이름 가져오기 // String shellname = MQConfig.getValueByKey(fileName); if( shellname!=null&&!"".equals(shellname)){// //쉘 호출 중// ReadCmdLine.callShell(셸 이름);// }else{// System.out.println("셸 이름이 없으며 파일만 수신합니다.");// } } public void runGoupReceiver() { try { init() getGroupMessages; (); qmgr.commit(); System.out.println("/n 메시지가 성공적으로 수신되었습니다. "); mqe) { mqe.printStackTrace(); try { System.out.println("/n 트랜잭션 취소") System.exit(2) } catch(예외 e); ); System.exit(2); } } catch (예외 e) { e.printStackTrace() } } /** * 서버 연결 정보 초기화 * * @throws 예외 */ private void init() throws Exception { /* 클라이언트 연결에 대한 MQEnvironment 속성 설정 */ MQEnvironment.hostname = 호스트; MQEnvironment.port = 채널; 큐 관리자에게*/ qmgr = new MQQueueManager(qmgrName) /* 입력에 대한 큐 열기 옵션 설정 */ int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING /* 입력에 대한 큐 열기 */ inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null); * Accept 파일의 주요 기능 * * @throws 예외 */ public void getGroupMessages() { /* 메시지 가져오기 옵션 설정 */ gmo = new MQGetMessageOptions(); gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING; gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT; / gmo.options = gmo.options + MQConstants.MQGMO_WAIT; /* 대기 시간 제한 설정 */ gmo.waitInterval = 5000; /* 메시지만 가져오기 */ gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE; gmo.options + MQConstants.MQGMO_LOGICAL_ORDER; gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID; /* 메시지 버퍼 생성*/ inMsg = new MQMessage(); try { FileOutputStream fos = null /* 프로세스 그룹 메시지*/ while (true) { try { inQueue.get(inMsg, gmo) ); if (fos == null) { 시도 { 파일 이름 = inMsg.getStringProperty("fileName"); String fileName_full = file_dir + RenameUtil.rename(fileName); fos = new FileOutputStream(new File(fileName_full)) int msgLength(); = 새 바이트[msgLength]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); /* 마지막 메시지 식별자인지 확인*/ char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out .println ("그룹의 마지막 메시지") } inMsg.clearMessage() } catch; (예외 e) { System.out .println("속성 없이 메시지를 수신합니다. 아무 것도 하지 않습니다!"); inMsg.clearMessage() } } else { int msgLength = inMsg.getMessageLength(); [msgLength]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); 마지막 메시지 식별자인지 확인합니다.*/ char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Last Msg in Group") break; } } catch (예외 e) { char x = gmo.groupStatus if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("그룹의 마지막 메시지") } break; } } if (fos != null) fos.close() } catch (예외 e) { System.out.println( e.getMessage()); } } public void initproperty() { MQConfig config = new MQConfig().getInstance(); if (config.getMQ_MANAGER() != null) { qmgrName = config.getMQ_MANAGER(); queueName = config.getMQ_QUEUE_NAME() 채널 = config.getMQ_CHANNEL(); ); 포트 = Integer.valueOf(config.getMQ_PROT()); ccsid = Integer.valueOf(config.getMQ_CCSID()) file_dir = config.getFILE_DIR();