メッセージキューイングアクセス機能 TP1/Message Queue - Access 使用の手引
コーディング例
// All Rights Reserved. Copyright (C) 2003, Hitachi, Ltd. /********************************************************** ** JMSSample1EJB.java ** **********************************************************/ import javax.jms.QueueConnectionFactory; import javax.jms.QueueConnection; import javax.jms.QueueSession; import javax.jms.QueueSender; import javax.jms.QueueReceiver; import javax.jms.Queue; import javax.jms.Session; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.EJBException; import javax.ejb.CreateException; import java.rmi.RemoteException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSSample1EJB implements SessionBean { private SessionContext sContext = null; private String clientName = null; private QueueConnection qConnection = null; private Queue queue = null; private static final String queueLookupName = "java:comp/env/jms/queue"; private static final String cfLookupName = "java:comp/env/jms/qcf"; /********************************************************** ** name = send() ** func = initialize a queue sender and send a message ** (1)createQueueSession(create a queue session) ** (2)createSender(create a queue sender) ** (3)send(send the message) ** (4)commit(commit local transaction) ** (5)close(close the queue sender) ** (6)close(close the queue session) **********************************************************/ public void send() { QueueSession qSession = null; QueueSender qSender = null; BytesMessage putMessage = null; try { // create a queue session qSession = qConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); // create a queue sender qSender = qSession.createSender(queue); // create a message object putMessage = qSession.createBytesMessage(); // UTF format message data putMessage. writeUTF("******** sample put data " + this.clientName + " ********"); // send the message qSender.send(putMessage); // commit local transaction qSession.commit(); // close the queue sender qSender.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in send(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } finally { if(qSession != null) { try { // close the queue session qSession.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in send(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } } } /********************************************************** ** name = receive() ** func = initialize a queue receiver and ** receive a message ** (1)start(start the queue connection) ** (2)createQueueSession(create a queue session) ** (3)createReceiver(create a queue receiver) ** (4)receive(receive a message) ** (5)commit(commit local transaction) ** (6)close(close the queue receiver) ** (7)close(close the queue session) **********************************************************/ public void receive() { QueueSession qSession = null; QueueReceiver qReceiver = null; BytesMessage getMessage = null; try { // start the queue connection qConnection.start(); // create a queue session qSession = qConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); // create a queue receiver qReceiver = qSession.createReceiver(queue); // receive a message getMessage = (BytesMessage)qReceiver.receive(1000); // commit local transaction qSession.commit(); if(getMessage != null) { // display the get message String msgText = getMessage.readUTF(); System.out.println("The message is: " + msgText); } // close the queue receiver qReceiver.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in receive(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } finally { if(qSession != null) { try { // close the queue session qSession.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in receive(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } } } /********************************************************** ** name = setSessionContext() ** func = set a session context **********************************************************/ public void setSessionContext(SessionContext sc) { this.sContext = sc; } /********************************************************** ** name = ejbCreate() ** func = ** (1)lookup(look up a connection factory and ** a queue object) ** (2)createQueueConnection(create a queue connection) **********************************************************/ public void ejbCreate(String name) throws CreateException { Context ic = null; QueueConnectionFactory qcFactory = null; this.clientName = name; try { // create a context ic = new InitialContext(); // looking up ConnectionFactory qcFactory = (QueueConnectionFactory)ic.lookup(cfLookupName); // look up a queue object queue = (Queue)ic.lookup(queueLookupName); // create a queue connection qConnection = qcFactory.createQueueConnection(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in ejbCreate(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new CreateException(ex.getMessage()); } catch (NamingException nex) { System.err.println("A NamingException was thrown: " + nex.getMessage()); nex.printStackTrace(); throw new CreateException(nex.getMessage()); } catch (Exception e) { System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new CreateException(e.getMessage()); } } /********************************************************** ** name = ejbRemove() ** func = ** (1)close(close the queue connection) **********************************************************/ public void ejbRemove() { try { if(qConnection != null) { // close the queue connection qConnection.close(); } } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in ejbRemove(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } public void ejbActivate() {} public void ejbPassivate() {} }
コーディング例(MQCインタフェース使用時)
// All Rights Reserved. Copyright (C) 2003, Hitachi, Ltd. /********************************************************** ** JMSSample2EJB.java ** **********************************************************/ import javax.jms.QueueConnectionFactory; import javax.jms.QueueConnection; import javax.jms.QueueSession; import javax.jms.QueueSender; import javax.jms.QueueReceiver; import javax.jms.Queue; import javax.jms.Session; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.EJBException; import javax.ejb.CreateException; import java.rmi.RemoteException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import jp.co.Hitachi.soft.mqadaptor.MQC; public class JMSSample2EJB implements SessionBean { private SessionContext sContext = null; private String clientName = null; private QueueConnection qConnection = null; private Queue queue = null; private static final String queueLookupName = "java:comp/env/jms/queue"; private static final String cfLookupName = "java:comp/env/jms/qcf"; /********************************************************** ** name = send() ** func = initialize a queue sender and send a message ** (1)createQueueSession(create a queue session) ** (2)createSender(create a queue sender) ** (3)send(send the message) ** (4)commit(commit local transaction) ** (5)close(close the queue sender) ** (6)close(close the queue session) **********************************************************/ public void send() { QueueSession qSession = null; QueueSender qSender = null; BytesMessage putMessage = null; try { // create a queue session qSession = qConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); // create a queue sender qSender = qSession.createSender(queue); // create a message object putMessage = qSession.createBytesMessage(); // UTF format message data putMessage. writeUTF("******** sample put data " + this.clientName + " ********"); // set a property putMessage.setStringProperty( "JMS_HITACHI_Format",MQC.MQFMT_STRING); // send the message qSender.send(putMessage); // commit local transaction qSession.commit(); // close the queue sender qSender.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in send(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } finally { if(qSession != null) { try { // close the queue session qSession.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in send(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } } } /********************************************************** ** name = receive() ** func = initialize a queue receiver and ** receive a message ** (1)start(start the queue connection) ** (2)createQueueSession(create a queue session) ** (3)createReceiver(create a queue receiver) ** (4)receive(receive a message) ** (5)commit(commit local transaction) ** (6)close(close the queue receiver) ** (7)close(close the queue session) **********************************************************/ public void receive() { QueueSession qSession = null; QueueReceiver qReceiver = null; BytesMessage getMessage = null; try { // start the queue connection qConnection.start(); // create a queue session qSession = qConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); // create a queue receiver qReceiver = qSession.createReceiver(queue); // receive a message getMessage = (BytesMessage)qReceiver.receive(1000); // commit local transaction qSession.commit(); if(getMessage != null) { // display the get message String msgText = getMessage.readUTF(); String msgFormat = getMessage. getStringProperty("JMS_HITACHI_Format"); System.out.println("The message is: " + msgText); System.out.println( "The message format is: " + msgFormat); } // close the queue receiver qReceiver.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in receive(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } finally { if(qSession != null) { try { // close the queue session qSession.close(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in receive(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } } } /********************************************************** ** name = setSessionContext() ** func = set a session context **********************************************************/ public void setSessionContext(SessionContext sc) { this.sContext = sc; } /********************************************************** ** name = ejbCreate() ** func = ** (1)lookup(look up a connection factory and ** a queue object) ** (2)createQueueConnection(create a queue connection) **********************************************************/ public void ejbCreate(String name) throws CreateException { Context ic = null; QueueConnectionFactory qcFactory = null; this.clientName = name; try { // create a context ic = new InitialContext(); // looking up ConnectionFactory qcFactory = (QueueConnectionFactory)ic.lookup(cfLookupName); // look up a queue object queue = (Queue)ic.lookup(queueLookupName); // create a queue connection qConnection = qcFactory.createQueueConnection(); } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in ejbCreate(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new CreateException(ex.getMessage()); } catch (NamingException nex) { System.err.println("A NamingException was thrown: " + nex.getMessage()); nex.printStackTrace(); throw new CreateException(nex.getMessage()); } catch (Exception e) { System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new CreateException(e.getMessage()); } } /********************************************************** ** name = ejbRemove() ** func = ** (1)close(close the queue connection) **********************************************************/ public void ejbRemove() { try { if(qConnection != null) { // close the queue connection qConnection.close(); } } catch (JMSException ex) { // JMS error System.out.println ("An error occurred in ejbRemove(): " + ex.getMessage() + " error code = " + ex.getErrorCode()); ex.printStackTrace(); throw new EJBException(ex.getMessage()); } catch(Exception e) { // other exception System.err.println("An exception was thrown: " + e.getMessage()); e.printStackTrace(); throw new EJBException(e.getMessage()); } } public void ejbActivate() {} public void ejbPassivate() {} }
All Rights Reserved. Copyright (C) 2006, 2007, Hitachi, Ltd.
(C) Copyright International Business Machines Corporation 1994, 1999. All rights reserved.