Ecriture de classes pour encapsuler une présentation d'enregistrement dans un JMSBytesMessage
L'objectif de cette tâche est d'explorer, par exemple, comment combiner la conversion de données et une présentation d'enregistrement fixe dans un JMSBytesMessage. Dans la tâche, vous créez des classes Java pour échanger un exemple de structure d'enregistrement dans un JMSBytesMessage. Vous pouvez modifier l'exemple pour écrire des classes afin d'échanger d'autres structures d'enregistrement.
Un JMSBytesMessage est le meilleur choix de type de message JMS pour échanger des enregistrements de type de données mixtes avec des programmes nonJMS . Aucune donnée supplémentaire n'est insérée dans le corps du message par le fournisseur JMS . Il s'agit donc du meilleur choix de type de message à utiliser si un programme client JMS interagit avec un programme IBM® MQ existant. Le défi principal de l'utilisation d'un JMSBytesMessage consiste à faire correspondre le codage et le jeu de caractères attendus par l'autre programme. Une solution consiste à créer une classe qui encapsule l'enregistrement. Une classe qui encapsule la lecture et l'écriture d'un JMSBytesMessage, pour un type d'enregistrement spécifique, facilite l'envoi et la réception d'enregistrements au format fixe dans un programme JMS . En capturant les aspects génériques de l'interface dans une classe abstraite, une grande partie de la solution peut être réutilisée pour différents formats d'enregistrement. Différents formats d'enregistrement peuvent être implémentés dans des classes qui étendent la classe générique abstraite.
Une autre approche consiste à étendre la classe com.ibm.mq.headers.Header . La classe Header comporte des méthodes, telles que addMQLONG, pour générer un format d'enregistrement de manière plus déclarative. L'utilisation de la classe Header présente l'inconvénient d'obtenir et de définir des attributs à l'aide d'une interface d'interprétation plus complexe. Les deux approches aboutissent à une quantité sensiblement identique de code d'application.
Un JMSBytesMessage ne peut encapsuler qu'un seul format, en plus d'un MQRFH2, dans un seul message, sauf si chaque enregistrement utilise le même format, le même jeu de caractères codés et le même codage. Le format, le codage et le jeu de caractères d'un JMSBytesMessage sont les propriétés de tous les messages qui suivent le MQRFH2. L'exemple est écrit en supposant qu'un JMSBytesMessage ne contient qu'un seul enregistrement utilisateur.
Avant de commencer
- Votre niveau de compétence: vous devez être familiarisé avec la programmation Java et JMS. Aucune instruction n'est fournie pour la configuration de l'environnement de développement Java . Il est avantageux d'avoir écrit un programme pour échanger un JMSTextMessage, un JMSStreamMessageou un JMSMapMessage. Vous pouvez ensuite voir les différences dans l'échange d'un message à l'aide d'un JMSBytesMessage.
- L'exemple requiert IBM WebSphere® MQ 7.0.
- L'exemple a été créé en utilisant la perspective Java de l'atelier Eclipse. Il nécessite JRE 6.0 ou une version plus récente. Vous pouvez utiliser la perspective Java dans IBM MQ Explorer pour développer et exécuter les classes Java . Vous pouvez également utiliser votre propre environnement de développement Java .
- L'utilisation de IBM MQ Explorer permet de configurer l'environnement de test et le débogage, plus simplement que l'utilisation des utilitaires de ligne de commande.
A propos de cette tâche
Vous êtes guidé lors de la création de deux classes: RECORD et MyRecord. Ensemble, ces deux classes encapsulent un enregistrement à format fixe. Ils possèdent des méthodes permettant d'obtenir et de définir des attributs. La méthode get lit l'enregistrement à partir d'un JMSBytesMessage et la méthode put écrit un enregistrement dans un JMSBytesMessage.
L'objectif de la tâche n'est pas de créer une classe de qualité de production que vous pouvez réutiliser. Vous pouvez choisir d'utiliser les exemples de la tâche pour commencer à utiliser vos propres classes. L'objectif de la tâche est de vous fournir des notes de conseils, principalement sur l'utilisation des jeux de caractères, des formats et du codage, lors de l'utilisation d'un JMSBytesMessage. Chaque étape de création des classes est expliquée et les aspects de l'utilisation de JMSBytesMessage, qui sont parfois négligés, sont décrits.
La classe RECORD est abstraite et définit des zones communes pour un enregistrement utilisateur. Les zones communes sont modélisées sur la présentation d'en-tête IBM MQ standard d'un identificateur, d'une version et d'une zone de longueur. Les zones de codage, de jeu de caractères et de format, qui se trouvent dans de nombreux en-têtes IBM MQ , sont omises. Un autre en-tête ne peut pas suivre un format défini par l'utilisateur. La classe MyRecord , qui étend la classe RECORD , le fait en étendant littéralement l'enregistrement avec des zones utilisateur supplémentaires. Un JMSBytesMessage, créé par les classes, peut être traité par l'exit de conversion des données du gestionnaire de files d'attente.
scaffoldingsupplémentaires pour tester RECORD et MyRecord. Les classes supplémentaires sont les suivantes:
- TryMyRecord
- Le programme principal pour tester RECORD et MyRecord.
- EndPoint
- Classe abstraite qui encapsule la connexion, la destination et la session JMS dans une seule classe. Son interface répond simplement aux besoins de test des classes RECORD et MyRecord . Il ne s'agit pas d'un modèle de conception établi pour l'écriture d'applications JMS .Remarque: la classe Endpoint inclut cette ligne de code après la création d'une destination:
((MQDestination)destination).setReceiveConversion (WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);Dans V7.0, à partir de V7.0.1.5, il est nécessaire d'activer la conversion du gestionnaire de files d'attente. Elle est désactivée par défaut. Dans V7.0, la conversion du gestionnaire de files d'attente jusqu'à V7.0.1.4 est activée par défaut et cette ligne de code génère une erreur.
- MyProducer et MyConsumer
- Classes qui étendent EndPointet créent un MessageConsumer et un MessageProducer, connectés et prêts à accepter des demandes.
Procédure
Résultats
- Résultats de l'exécution de la classe TryMyRecord :
- Envoi d'un message dans le jeu de caractères codés 37 et utilisation d'un exit de conversion de gestionnaire de files d'attente:
Out flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 546 CCSID 37 MQ true Out flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 546 CCSID 37 MQ true In flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 273 CCSID UTF-8 - Envoi d'un message dans le jeu de caractères codés 37 et non utilisation d'un exit de conversion de gestionnaire de files d'attente:
Out flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 546 CCSID 37 MQ true Out flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 546 CCSID 37 MQ true In flags 1 text ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 Encoding 546 CCSID IBM037
- Envoi d'un message dans le jeu de caractères codés 37 et utilisation d'un exit de conversion de gestionnaire de files d'attente:
- Les résultats de la modification de la classe TryMyRecord pour ne pas recevoir le message et de sa réception à l'aide de l'exemple amqsget0.c modifié. L'exemple modifié accepte un enregistrement formaté ; voir la figure 2 dans l'échange d'un enregistrement formaté avec une application non JMS.
- Envoi d'un message dans le jeu de caractères codés 37 et utilisation d'un exit de conversion de gestionnaire de files d'attente:
Sample AMQSGET0 start ccsid <850>, flags <1>, message <ABCDEFGHIJKLMNOPQRSTUVWXYZ012345> no more messages Sample AMQSGET0 end - Envoi d'un message dans le jeu de caractères codés 37 et non utilisation d'un exit de conversion de gestionnaire de files d'attente:
Sample AMQSGET0 start MQGET ended with reason code 2110 ccsid <37>, flags <1>, message <--+-+ãÃ++ÐÊËÈiÐÎÐ+ÔÒõõμþÞÚ-±=¾¶§> no more messages Sample AMQSGET0 end
- Envoi d'un message dans le jeu de caractères codés 37 et utilisation d'un exit de conversion de gestionnaire de files d'attente:
Pour tester l'exemple et expérimenter avec différentes pages de codes et un exit de conversion de données. Créez les classes Java , configurez IBM MQet exécutez le programme principal, TryMyRecord ; voir #q032150___Try.
- Configurez IBM MQ et JMS pour exécuter l'exemple. Les instructions permettent d'exécuter l'exemple sous Windows.
- Création d'un gestionnaire de files d'attente
crtmqm -sa -u SYSTEM.DEAD.LETTER.QUEUE QM1 strmqm QM1 - Création d'une file d'attente
echo DEFINE QL('Q1') REPLACE | runmqsc QM1 - Créer un répertoire JNDI
cd c:\ md JNDI-Directory - Passez dans le répertoire bin JMS
Le programme d'administration JMS doit être exécuté à partir d'ici. Le chemin d'accès est MQ_INSTALLATION_PATH\java\bin.
- Créez les définitions JMS suivantes dans un fichier appelé JMSQM1Q1.txt
DEF CF(QM1) PROVIDERVERSION(7) QMANAGER(QM1) DEF Q(Q1) CCSID(37) ENCODING(RRR) MSGBODY(MQ) QMANAGER(QM1) QUEUE(Q1) TARGCLIENT(MQ) VERSION(7) END - Exécutez le programme JMSAdmin pour créer les ressources JMS
JMSAdmin < JMSQM1Q1.txt
- Création d'un gestionnaire de files d'attente
- Vous pouvez créer, modifier et parcourir les définitions que vous avez créées à l'aide de l'explorateur IBM MQ .
- Exécutez TryMyRecord.
Classes utilisées pour exécuter l'exemple
Les classes répertoriées dans les blocs de code suivants sont également disponibles dans un fichier compressé. Téléchargez jm25529_.zip ou jm25529_.tar.gz.package com.ibm.mq.id;
public class TryMyRecord {
public static void main(String[] args) throws Exception {
MyProducer producer = new MyProducer();
MyRecord outrec = new MyRecord();
System.out.println("Out flags " + outrec.getFlags() + " text "
+ outrec.getRecordData() + " Encoding "
+ producer.getEncoding() + " CCSID " + producer.getCCSID()
+ " MQ " + producer.getMQDest());
outrec.put(producer);
System.out.println("Out flags " + outrec.getFlags() + " text "
+ outrec.getRecordData() + " Encoding "
+ producer.getEncoding() + " CCSID " + producer.getCCSID()
+ " MQ " + producer.getMQDest());
MyRecord inrec = MyRecord.get(new MyConsumer());
System.out.println("In flags " + inrec.getFlags() + " text "
+ inrec.getRecordData() + " Encoding "
+ inrec.getMessageEncoding() + " CCSID "
+ inrec.getMessageCharset());
}
}
![[ Messagerie Jakarta 3.0]](ngjm30.gif)
package com.ibm.mq.id;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import jakarta.jms.BytesMessage;
import jakarta.jms.JMSException;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.MQDataException;
import com.ibm.msg.client.wmq.WMQConstants;
public abstract class RECORD implements Serializable {
private static final long serialVersionUID = -1616617232750561712L;
protected final static int UTF8 = 1208;
protected final static int MQLONG_LENGTH = 4;
protected final static int RECORD_STRUCT_ID_LENGTH = 4;
protected final static int RECORD_VERSION_1 = 1;
protected final String RECORD_STRUCT_ID = "BLNK";
protected final String RECORD_TYPE = "BLANK ";
private String structID = RECORD_STRUCT_ID;
private int version = RECORD_VERSION_1;
private int structLength = RECORD_STRUCT_ID_LENGTH + MQLONG_LENGTH * 2;
private int headerEncoding = WMQConstants.WMQ_ENCODING_NATIVE;
private String headerCharset = "UTF-8";
private String headerFormat = RECORD_TYPE;
public RECORD() {
super();
}
public RECORD(BytesMessage message) throws JMSException, IOException,
MQDataException {
super();
setHeaderCharset(message.getStringProperty(WMQConstants.JMS_IBM_CHARACTER_SET));
setHeaderEncoding(message.getIntProperty(WMQConstants.JMS_IBM_ENCODING));
byte[] structID = new byte[RECORD_STRUCT_ID_LENGTH];
message.readBytes(structID, RECORD_STRUCT_ID_LENGTH);
setStructID(new String(structID, getMessageCharset()));
setVersion(message.readInt());
setStructLength(message.readInt());
}
public String getHeaderFormat() { return headerFormat; }
public int getHeaderEncoding() { return headerEncoding; }
public String getMessageCharset() { return headerCharset; }
public int getMessageEncoding() { return headerEncoding; }
public String getStructID() { return structID; }
public int getStructLength() { return structLength; }
public int getVersion() { return version; }
protected BytesMessage put(MyProducer myProducer) throws IOException,
JMSException, UnsupportedEncodingException {
setHeaderEncoding(myProducer.getEncoding());
setHeaderCharset(myProducer.getCharset());
myProducer.setMQClient(true);
BytesMessage bytes = myProducer.session.createBytesMessage();
bytes.setStringProperty(WMQConstants.JMS_IBM_FORMAT, getHeaderFormat());
bytes.setIntProperty(WMQConstants.JMS_IBM_ENCODING, getHeaderEncoding());
bytes.setIntProperty(WMQConstants.JMS_IBM_CHARACTER_SET,
myProducer.getCCSID());
bytes.writeBytes(String.format("%1$-" + RECORD_STRUCT_ID_LENGTH + "."
+ RECORD_STRUCT_ID_LENGTH + "s", getStructID())
.getBytes(getMessageCharset()), 0, RECORD_STRUCT_ID_LENGTH);
bytes.writeInt(getVersion());
bytes.writeInt(getStructLength());
return bytes;
}
public void setHeaderCharset(String charset) {
this.headerCharset = charset; }
public void setHeaderEncoding(int encoding) {
this.headerEncoding = encoding; }
public void setHeaderFormat(String headerFormat) {
this.headerFormat = headerFormat; }
public void setStructID(String structID) {
this.structID = structID; }
public void setStructLength(int structLength) {
this.structLength = structLength; }
public void setVersion(int version) {
this.version = version; }
}
![[ JMS 2.0]](ngjms20.gif)
package com.ibm.mq.id;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.MQDataException;
import com.ibm.msg.client.wmq.WMQConstants;
public abstract class RECORD implements Serializable {
private static final long serialVersionUID = -1616617232750561712L;
protected final static int UTF8 = 1208;
protected final static int MQLONG_LENGTH = 4;
protected final static int RECORD_STRUCT_ID_LENGTH = 4;
protected final static int RECORD_VERSION_1 = 1;
protected final String RECORD_STRUCT_ID = "BLNK";
protected final String RECORD_TYPE = "BLANK ";
private String structID = RECORD_STRUCT_ID;
private int version = RECORD_VERSION_1;
private int structLength = RECORD_STRUCT_ID_LENGTH + MQLONG_LENGTH * 2;
private int headerEncoding = WMQConstants.WMQ_ENCODING_NATIVE;
private String headerCharset = "UTF-8";
private String headerFormat = RECORD_TYPE;
public RECORD() {
super();
}
public RECORD(BytesMessage message) throws JMSException, IOException,
MQDataException {
super();
setHeaderCharset(message.getStringProperty(WMQConstants.JMS_IBM_CHARACTER_SET));
setHeaderEncoding(message.getIntProperty(WMQConstants.JMS_IBM_ENCODING));
byte[] structID = new byte[RECORD_STRUCT_ID_LENGTH];
message.readBytes(structID, RECORD_STRUCT_ID_LENGTH);
setStructID(new String(structID, getMessageCharset()));
setVersion(message.readInt());
setStructLength(message.readInt());
}
public String getHeaderFormat() { return headerFormat; }
public int getHeaderEncoding() { return headerEncoding; }
public String getMessageCharset() { return headerCharset; }
public int getMessageEncoding() { return headerEncoding; }
public String getStructID() { return structID; }
public int getStructLength() { return structLength; }
public int getVersion() { return version; }
protected BytesMessage put(MyProducer myProducer) throws IOException,
JMSException, UnsupportedEncodingException {
setHeaderEncoding(myProducer.getEncoding());
setHeaderCharset(myProducer.getCharset());
myProducer.setMQClient(true);
BytesMessage bytes = myProducer.session.createBytesMessage();
bytes.setStringProperty(WMQConstants.JMS_IBM_FORMAT, getHeaderFormat());
bytes.setIntProperty(WMQConstants.JMS_IBM_ENCODING, getHeaderEncoding());
bytes.setIntProperty(WMQConstants.JMS_IBM_CHARACTER_SET,
myProducer.getCCSID());
bytes.writeBytes(String.format("%1$-" + RECORD_STRUCT_ID_LENGTH + "."
+ RECORD_STRUCT_ID_LENGTH + "s", getStructID())
.getBytes(getMessageCharset()), 0, RECORD_STRUCT_ID_LENGTH);
bytes.writeInt(getVersion());
bytes.writeInt(getStructLength());
return bytes;
}
public void setHeaderCharset(String charset) {
this.headerCharset = charset; }
public void setHeaderEncoding(int encoding) {
this.headerEncoding = encoding; }
public void setHeaderFormat(String headerFormat) {
this.headerFormat = headerFormat; }
public void setStructID(String structID) {
this.structID = structID; }
public void setStructLength(int structLength) {
this.structLength = structLength; }
public void setVersion(int version) {
this.version = version; }
}
![[ Messagerie Jakarta 3.0]](ngjm30.gif)
package com.ibm.mq.id;
import java.io.IOException;
import jakarta.jms.BytesMessage;
import jakarta.jms.JMSException;
import com.ibm.mq.headers.MQDataException;
public class MyRecord extends RECORD {
private static final long serialVersionUID = -370551723162299429L;
private final static int FLAGS = 1;
private final static String STRUCT_ID = "MYRD";
private final static int DATA_LENGTH = 32;
private final static String FORMAT = "MYRECORD";
private int flags = FLAGS;
private String recordData = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
public MyRecord() {
super();
super.setStructID(STRUCT_ID);
super.setHeaderFormat(FORMAT);
super.setStructLength(super.getStructLength() + MQLONG_LENGTH
+ DATA_LENGTH);
}
public MyRecord(BytesMessage message) throws JMSException, IOException,
MQDataException {
super(message);
setFlags(message.readInt());
byte[] recordData = new byte[DATA_LENGTH];
message.readBytes(recordData, DATA_LENGTH);
setRecordData(new String(recordData, super.getMessageCharset()));
}
public static MyRecord get(MyConsumer myConsumer) throws JMSException,
MQDataException, IOException {
BytesMessage message = (BytesMessage) myConsumer.receive();
return new MyRecord(message);
}
public int getFlags() { return flags; }
public String getRecordData() { return recordData; } .
public BytesMessage put(MyProducer myProducer) throws JMSException,
IOException {
BytesMessage bytes = super.put(myProducer);
bytes.writeInt(getFlags());
bytes.writeBytes(String.format("%1$-" + DATA_LENGTH + "."
+ DATA_LENGTH + "s",getRecordData())
.getBytes(super.getMessageCharset()), 0, DATA_LENGTH);
myProducer.send(bytes);
return bytes;
}
public void setFlags(int flags) {
this.flags = flags; }
public void setRecordData(String recordData) {
this.recordData = recordData; }
}
![[ JMS 2.0]](ngjms20.gif)
package com.ibm.mq.id;
import java.io.IOException;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import com.ibm.mq.headers.MQDataException;
public class MyRecord extends RECORD {
private static final long serialVersionUID = -370551723162299429L;
private final static int FLAGS = 1;
private final static String STRUCT_ID = "MYRD";
private final static int DATA_LENGTH = 32;
private final static String FORMAT = "MYRECORD";
private int flags = FLAGS;
private String recordData = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
public MyRecord() {
super();
super.setStructID(STRUCT_ID);
super.setHeaderFormat(FORMAT);
super.setStructLength(super.getStructLength() + MQLONG_LENGTH
+ DATA_LENGTH);
}
public MyRecord(BytesMessage message) throws JMSException, IOException,
MQDataException {
super(message);
setFlags(message.readInt());
byte[] recordData = new byte[DATA_LENGTH];
message.readBytes(recordData, DATA_LENGTH);
setRecordData(new String(recordData, super.getMessageCharset()));
}
public static MyRecord get(MyConsumer myConsumer) throws JMSException,
MQDataException, IOException {
BytesMessage message = (BytesMessage) myConsumer.receive();
return new MyRecord(message);
}
public int getFlags() { return flags; }
public String getRecordData() { return recordData; } .
public BytesMessage put(MyProducer myProducer) throws JMSException,
IOException {
BytesMessage bytes = super.put(myProducer);
bytes.writeInt(getFlags());
bytes.writeBytes(String.format("%1$-" + DATA_LENGTH + "."
+ DATA_LENGTH + "s",getRecordData())
.getBytes(super.getMessageCharset()), 0, DATA_LENGTH);
myProducer.send(bytes);
return bytes;
}
public void setFlags(int flags) {
this.flags = flags; }
public void setRecordData(String recordData) {
this.recordData = recordData; }
}
![[ Messagerie Jakarta 3.0]](ngjm30.gif)
package com.ibm.mq.id;
import java.io.UnsupportedEncodingException;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.mq.headers.CCSID;
import com.ibm.mq.jms.MQDestination;
import com.ibm.msg.client.wmq.WMQConstants;
public abstract class EndPoint {
public Context ctx;
public ConnectionFactory cf;
public Connection connection;
public Destination destination;
public Session session;
protected EndPoint() throws NamingException, JMSException {
System.setProperty("java.naming.provider.url", "file:/C:/JNDI-Directory");
System.setProperty("java.naming.factory.initial",
"com.sun.jndi.fscontext.RefFSContextFactory");
ctx = new InitialContext();
cf = (ConnectionFactory) ctx.lookup("QM1");
connection = cf.createConnection();
destination = (Destination) ctx.lookup("Q1");
((MQDestination)destination).setReceiveConversion
(WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); }
protected EndPoint(String cFactory, String dest) throws NamingException,
JMSException {
System.setProperty("java.naming.provider.url", "file:/C:/JNDI-Directory");
System.setProperty("java.naming.factory.initial",
"com.sun.jndi.fscontext.RefFSContextFactory");
ctx = new InitialContext();
cf = (ConnectionFactory) ctx.lookup(cFactory);
connection = cf.createConnection();
destination = (Destination) ctx.lookup(dest);
((MQDestination)destination).setReceiveConversion
(WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); }
public int getCCSID() throws JMSException {
return (((MQDestination) destination)
.getIntProperty(WMQConstants.WMQ_CCSID)); }
public String getCharset() throws UnsupportedEncodingException,
JMSException {
return CCSID.getCodepage(getCCSID()); }
public int getEncoding() throws JMSException {
return (((MQDestination) destination)
.getIntProperty(WMQConstants.WMQ_ENCODING)); }
public boolean getMQDest() throws JMSException {
if ((((MQDestination) destination).getMessageBodyStyle()
== WMQConstants.WMQ_MESSAGE_BODY_MQ)
|| ((((MQDestination) destination).getMessageBodyStyle()
== WMQConstants.WMQ_MESSAGE_BODY_UNSPECIFIED)
&& (((MQDestination) destination).getTargetClient()
== WMQConstants.WMQ_TARGET_DEST_MQ)))
return true;
else
return false; }
public void setCCSID(int ccsid) throws JMSException {
((MQDestination) destination).setIntProperty(WMQConstants.WMQ_CCSID,
ccsid); }
public void setEncoding(int encoding) throws JMSException {
((MQDestination) destination).setIntProperty(WMQConstants.WMQ_ENCODING,
encoding); }
public void setMQBody() throws JMSException {
((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_UNSPECIFIED); }
public void setMQBody(boolean mqbody) throws JMSException {
if (mqbody) ((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_MQ);
else ((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_JMS); }
public void setMQClient(boolean mqclient) throws JMSException {
if (mqclient){
((MQDestination) destination).setTargetClient(WMQConstants.WMQ_TARGET_DEST_MQ);
if (!getMQDest()) setMQBody();
}
else
((MQDestination) destination).setTargetClient(WMQConstants.WMQ_TARGET_DEST_JMS); }
}
![[ JMS 2.0]](ngjms20.gif)
package com.ibm.mq.id;
import java.io.UnsupportedEncodingException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.mq.headers.CCSID;
import com.ibm.mq.jms.MQDestination;
import com.ibm.msg.client.wmq.WMQConstants;
public abstract class EndPoint {
public Context ctx;
public ConnectionFactory cf;
public Connection connection;
public Destination destination;
public Session session;
protected EndPoint() throws NamingException, JMSException {
System.setProperty("java.naming.provider.url", "file:/C:/JNDI-Directory");
System.setProperty("java.naming.factory.initial",
"com.sun.jndi.fscontext.RefFSContextFactory");
ctx = new InitialContext();
cf = (ConnectionFactory) ctx.lookup("QM1");
connection = cf.createConnection();
destination = (Destination) ctx.lookup("Q1");
((MQDestination)destination).setReceiveConversion
(WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); }
protected EndPoint(String cFactory, String dest) throws NamingException,
JMSException {
System.setProperty("java.naming.provider.url", "file:/C:/JNDI-Directory");
System.setProperty("java.naming.factory.initial",
"com.sun.jndi.fscontext.RefFSContextFactory");
ctx = new InitialContext();
cf = (ConnectionFactory) ctx.lookup(cFactory);
connection = cf.createConnection();
destination = (Destination) ctx.lookup(dest);
((MQDestination)destination).setReceiveConversion
(WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); }
public int getCCSID() throws JMSException {
return (((MQDestination) destination)
.getIntProperty(WMQConstants.WMQ_CCSID)); }
public String getCharset() throws UnsupportedEncodingException,
JMSException {
return CCSID.getCodepage(getCCSID()); }
public int getEncoding() throws JMSException {
return (((MQDestination) destination)
.getIntProperty(WMQConstants.WMQ_ENCODING)); }
public boolean getMQDest() throws JMSException {
if ((((MQDestination) destination).getMessageBodyStyle()
== WMQConstants.WMQ_MESSAGE_BODY_MQ)
|| ((((MQDestination) destination).getMessageBodyStyle()
== WMQConstants.WMQ_MESSAGE_BODY_UNSPECIFIED)
&& (((MQDestination) destination).getTargetClient()
== WMQConstants.WMQ_TARGET_DEST_MQ)))
return true;
else
return false; }
public void setCCSID(int ccsid) throws JMSException {
((MQDestination) destination).setIntProperty(WMQConstants.WMQ_CCSID,
ccsid); }
public void setEncoding(int encoding) throws JMSException {
((MQDestination) destination).setIntProperty(WMQConstants.WMQ_ENCODING,
encoding); }
public void setMQBody() throws JMSException {
((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_UNSPECIFIED); }
public void setMQBody(boolean mqbody) throws JMSException {
if (mqbody) ((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_MQ);
else ((MQDestination) destination)
.setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_JMS); }
public void setMQClient(boolean mqclient) throws JMSException {
if (mqclient){
((MQDestination) destination).setTargetClient(WMQConstants.WMQ_TARGET_DEST_MQ);
if (!getMQDest()) setMQBody();
}
else
((MQDestination) destination).setTargetClient(WMQConstants.WMQ_TARGET_DEST_JMS); }
}
![[ Messagerie Jakarta 3.0]](ngjm30.gif)
package com.ibm.mq.id;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageProducer;
import javax.naming.NamingException;
public class MyProducer extends EndPoint {
public MessageProducer producer;
public MyProducer() throws NamingException, JMSException {
super();
producer = session.createProducer(destination); }
public MyProducer(String cFactory, String dest) throws NamingException,
JMSException {
super(cFactory, dest);
producer = session.createProducer(destination); }
public void send(Message message) throws JMSException {
producer.send(message); }
}
![[ JMS 2.0]](ngjms20.gif)
package com.ibm.mq.id;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.naming.NamingException;
public class MyProducer extends EndPoint {
public MessageProducer producer;
public MyProducer() throws NamingException, JMSException {
super();
producer = session.createProducer(destination); }
public MyProducer(String cFactory, String dest) throws NamingException,
JMSException {
super(cFactory, dest);
producer = session.createProducer(destination); }
public void send(Message message) throws JMSException {
producer.send(message); }
}
![[ Messagerie Jakarta 3.0]](ngjm30.gif)
package com.ibm.mq.id;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import javax.naming.NamingException;
public class MyConsumer extends EndPoint {
public MessageConsumer consumer;
public MyConsumer() throws NamingException, JMSException {
super();
consumer = session.createConsumer(destination);
connection.start(); }
public MyConsumer(String cFactory, String dest) throws NamingException,
JMSException {
super(cFactory, dest);
consumer = session.createConsumer(destination);
connection.start(); }
public Message receive() throws JMSException {
return consumer.receive(); }
}
![[ JMS 2.0]](ngjms20.gif)
package com.ibm.mq.id;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.naming.NamingException;
public class MyConsumer extends EndPoint {
public MessageConsumer consumer;
public MyConsumer() throws NamingException, JMSException {
super();
consumer = session.createConsumer(destination);
connection.start(); }
public MyConsumer(String cFactory, String dest) throws NamingException,
JMSException {
super(cFactory, dest);
consumer = session.createConsumer(destination);
connection.start(); }
public Message receive() throws JMSException {
return consumer.receive(); }
}