jpos :如何处理具有不同 headers 且长度不同的消息,监听相同的端点并使用不同的包装器

jpos : how to handle messages with different headers with different length listening to the same endpoint and using different packager

这不是一个重复的问题,因为上一个问题的某些功能发生了变化,并且这是下面问题的扩展。创建了一个新的 post,对其他寻求不同解决方案的人也很有帮助

场景是第三方应用程序将消息发送到jpos。可能是它会发送两种消息 header 长度不同的消息,比如一种长度为 22 字节,另一种长度为 44 bytes.the 打包程序将不同。

按照上述解决方案中的建议,我创建了一个自定义渠道并且它有效 fine.but 现在的挑战是我如何为每个不同的请求类型使用不同的打包程序

示例

header1 -- 使用 x 打包器

header2 -- 使用 y packager

如果是这样,我是否必须创建新的打包程序?我如何才能 link 服务器和打包程序以及 header 类型。 看起来非常具有挑战性,jpos 变得非常有趣。

    <property name="packager-config"  value="cfg/packager/CISebcdic.xml"="True" />


    <server class="org.jpos.q2.iso.QServer" logger="Q2" name="gwmip-server-7003" realm="bnet-server-8000">
        <attr name="port" type="java.lang.Integer">7003</attr>
        <channel class="org.jpos.iso.channel.NACChannel"
                 packager="org.jpos.iso.packager.GenericPackager"
                 type="server"
                 logger="Q2"

                 >
         <property name="packager-config"  value="cfg/packager/CISebcdic.xml" debug="True" />
            <property name="timeout" value="180000"/>
        </channel>
        <request-listener class="org.jpos.iso.IncomingListener" logger="Q2" realm="incoming-request-listener">
            <property name="queue"  value="GWMIPTXNMGR" />
            <property name="ctx.DESTINATION"  value="jPOS-AUTORESPONDER" />
        </request-listener>
    </server>

请帮忙,

提前致谢, 下面完全丢失的是我正在尝试的那个,问题出在

protected ISOPackager getDynamicPackager(byte[] header, byte[] message)

我收到如下错误

<log realm="gwmip-server-5281.server.session/10.157.152.95:26215" at="2020-04-18T00:52:22.018">
  <session-error>
    <exception name="unexpected exception">
    java.io.IOException: unexpected exception
        at org.jpos.iso.BaseChannel.receive(BaseChannel.java:787)
        at org.jpos.iso.ISOServer$Session.run(ISOServer.java:344)
        at org.jpos.util.ThreadPool$PooledThread.run(ThreadPool.java:76)
Caused by: java.lang.NullPointerException
        at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:70)
        at java.nio.ByteBuffer.wrap(ByteBuffer.java:373)
        at org.jpos.iso.channel.GWMipChannel.getDynamicPackager(GWMipChannel.java:73)
        at org.jpos.iso.BaseChannel.receive(BaseChannel.java:743)
        ... 2 more

已更新自定义频道

    package org.jpos.iso.channel;

    import org.jpos.core.Configuration;
    import org.jpos.core.ConfigurationException;
    import org.jpos.iso.ISOException;
    import org.jpos.iso.ISOMsg;
    import org.jpos.iso.ISOPackager;
    import org.jpos.iso.packager.GenericPackager;
    import org.jpos.iso.packager.MadaPackager;

    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.util.Arrays;

    public class GWMipChannel extends NACChannel {


        byte[] madaHeader = {(byte) 0x00, (byte) 0x18, (byte) 0xd6, (byte) 0xf3, (byte) 0xf6, (byte) 0xf8, (byte) 0xf7};
        byte[] nitmxHeader = {(byte) 0x00, (byte) 0x18, (byte) 0xd6, (byte) 0xf3, (byte) 0xf7, (byte) 0xf3, (byte) 0xf2};
        byte[] response_mada_header = {(byte) 0x00, (byte) 0x31, (byte) 0xc9, (byte) 0xf3, (byte) 0xf6, (byte) 0xf8, (byte) 0xf7};



        @Override
        protected byte[] readHeader(int hLen) throws IOException {

            byte[] header = new byte[25];
            serverIn.readFully(header, 0, 25);


             boolean mada_header = ByteBuffer.wrap(header, 0, 7).equals(ByteBuffer.wrap(madaHeader));
             boolean nitmx_header = ByteBuffer.wrap(header, 0, 7).equals(ByteBuffer.wrap(nitmxHeader));
             boolean rsp_mada_header = ByteBuffer.wrap(header, 0, 7).equals(ByteBuffer.wrap(response_mada_header, 0, 7));
            ISOPackager packager;
            if (mada_header) {

                return header;
            } else if (nitmx_header) {

                header = Arrays.copyOf(header,47 );

                serverIn.readFully(header, 25, 22);

                return header;
            } else if (rsp_mada_header) {

                //serverIn.readFully(header, 0, 50);

                header = Arrays.copyOf(header, 48);

                serverIn.readFully(header, 0, 48);

                return header;

            }

            return header;
        }
    @Override
        protected void sendMessageHeader(ISOMsg m, int len) throws IOException {
            byte[] header = m.getHeader();
            //assume header is the one to send, and already has 22 or 44 length
            //or you can check
            serverOut.write(header);


        }

       @Override
        protected ISOPackager getDynamicPackager(byte[] header, byte[] message) {

            boolean mada_header = ByteBuffer.wrap(header, 0, 7).equals(ByteBuffer.wrap(madaHeader,0,7));
            boolean nitmx_header = ByteBuffer.wrap(header, 0, 7).equals(ByteBuffer.wrap(nitmxHeader,0,7));
            ISOPackager packager = null;

            if(mada_header) {

             try {
                    packager = new GenericPackager("cfg/packager/CISebcidic_mada.xml");
                } catch (ISOException e) {
                    e.printStackTrace();
                }

            }
             else if (nitmx_header) {
                try {
                     packager = new GenericPackager("cfg/packager/AS2805_NITMX.xml");

                } catch (ISOException e) {
                    e.printStackTrace();
                }
            } else {

                Exception e = new ISOException();
                e.printStackTrace();
                //return some default or throw some runtime exception exception
            }

            return packager;
        }
    }

服务器配置文件是

    <server class="org.jpos.q2.iso.QServer" logger="Q2" name="gwmip-server-5281" realm="gwmip-server-5281">
        <attr name="port" type="java.lang.Integer">5281</attr>
        <channel class="org.jpos.iso.channel.GWMipChannel"
                 packager="org.jpos.iso.packager.GenericPackager"
                 type="server"
                 logger="Q2"
                 header="0018D6F3F7F3F2000000000000000000000000000000000002"
        >
            <property name="packager-config"  value="cfg/packager/AS2805_NITMX.xml" debug="True" />
            <property name="timeout" value="180000"/>
        </channel>
        <request-listener class="org.jpos.iso.IncomingListener" logger="Q2" realm="incoming-request-listener">
            <property name="queue"  value="GWMIPTXNMGR" />
            <property name="ctx.DESTINATION"  value="gwmip-AUTORESPONDER" />
        </request-listener>
    </server>

在您的频道自定义频道实现中,您可以覆盖 getDynamicPackager(byte[] header, byte[] message)


//instantiate each as required in constructor for instance or you can make them configurable through setConfiguration
protected ISOPackager packager22, packager44;
protected byte[] len22HeaderPrefix, len44HeaderPrefix;
protected int headerPrefixLen = 4; //4 according to your other question

protected getDynamicPackager(byte[] header, byte[] message) {

        if (Arrays.equals(header, len22HeaderPrefix, 0, prefixLen)) {
            return packager22;
        } else if (Arrays.equals(header, len44HeaderPrefix, 0, headerPrefixLen)) {
            return packager44;
        } else {
            //return some default or throw some runtime exception exception 
        }

}