2 yubao2008 yubao2008 于 2016.01.23 20:08 提问

用mina 客户端向第三方服务器发送消息时收不到返回报文

package com.demo.single;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.demo1.BaseConfig;
import com.demo1.ProviderClientHander;

public class ToolSendClient {

public static void main(String[] args) {
    String msg = "0000003076<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><sys-header><data name=\"SYS_HEAD\"><struct><data name=\"APPR_USER_ID\"><field length=\"30\" scale=\"0\" type=\"string\"/></data><data name=\"SEQ_NO\"><field length=\"14\" scale=\"0\" type=\"string\">9095256682051</field></data><data name=\"TRAN_CODE\"><field length=\"8\" scale=\"0\" type=\"string\"/></data><data name=\"TRAN_DATE\"><field length=\"8\" scale=\"0\" type=\"string\">20150720</field></data><data name=\"MESSAGE_CODE\"><field length=\"4\" scale=\"0\" type=\"string\">0407</field></data><data name=\"MODULE_ID\"><field length=\"2\" scale=\"0\" type=\"string\">RB</field></data><data name=\"TRAN_TYPE\"><field length=\"20\" scale=\"0\" type=\"string\"/></data><data name=\"USER_ID\"><field length=\"30\" scale=\"0\" type=\"string\">V0016</field></data><data name=\"MESSAGE_TYPE\"><field length=\"4\" scale=\"0\" type=\"string\">1400</field></data><data name=\"AUTH_PASSWORD\"><field length=\"0\" scale=\"0\" type=\"string\"/></data><data name=\"PROGRAM_ID\"><field length=\"20\" scale=\"0\" type=\"string\">FM144D</field></data><data name=\"SERVICE_CODE\"><field length=\"30\" scale=\"0\" type=\"string\">SVR_INQUIRY</field></data><data name=\"SERVER_ID\"><field length=\"30\" scale=\"0\" type=\"string\">127.0.0.1</field></data><data name=\"BRANCH_ID\"><field length=\"6\" scale=\"0\" type=\"string\">50801</field></data><data name=\"SOURCE_BRANCH_NO\"><field length=\"50\" scale=\"0\" type=\"string\">000011</field></data><data name=\"REVERSAL_TRAN_TYPE\"><field length=\"0\" scale=\"0\" type=\"string\"/></data><data name=\"WS_ID\"><field length=\"200\" scale=\"0\" type=\"string\">01</field></data><data name=\"SOURCE_TYPE\"><field length=\"2\" scale=\"0\" type=\"string\">HB</field></data><data name=\"DEST_BRANCH_NO\"><field length=\"6\" scale=\"0\" type=\"string\">000002</field></data><data name=\"USER_LANG\"><field length=\"100\" scale=\"0\" type=\"string\">CHINESE</field></data><data name=\"TRAN_TIMESTAMP\"><field length=\"9\" scale=\"0\" type=\"string\">165745418</field></data><data name=\"AUTH_FLAG\"><field length=\"4\" scale=\"0\" type=\"string\">N</field></data><data name=\"TRAN_MODE\"><field length=\"1\" scale=\"0\" type=\"string\">ONLINE</field></data><data name=\"AUTH_USER_ID\"><field length=\"30\" scale=\"0\" type=\"string\"/></data><data name=\"APPR_FLAG\"><field length=\"1\" scale=\"0\" type=\"string\"/></data></struct></data></sys-header><app-header><data name=\"APP_HEAD\"><struct><data name=\"PGUP_OR_PGDN\"><field length=\"15\" scale=\"0\" type=\"string\">1</field></data><data name=\"TOTAL_NUM\"><field length=\"15\" scale=\"0\" type=\"string\">10</field></data><data name=\"CURRENT_NUM\"><field length=\"15\" scale=\"0\" type=\"string\">0</field></data><data name=\"PAGE_START\"><field length=\"30\" scale=\"0\" type=\"string\">1</field></data><data name=\"PAGE_END\"><field length=\"15\" scale=\"0\" type=\"string\">10</field></data></struct></data></app-header><local-header/><body><data name=\"BASE_ACCT_NO\"><field length=\"50\" scale=\"0\" type=\"string\">6236220599200002847</field></data><data name=\"CARD_NO\"><field length=\"20\" scale=\"0\" type=\"string\"/></data><data name=\"PASSWORD\"><field length=\"50\" scale=\"0\" type=\"string\"/></data><data name=\"IC_CARD_SEQ\"><field length=\"3\" scale=\"0\" type=\"string\"/></data></body></service>";
    send2(msg);
}

public static void send2(String message) {
    TextLineCodecFactory lineCodec = new TextLineCodecFactory(
            Charset.forName("UTF-8"), LineDelimiter.UNIX.getValue(),
            LineDelimiter.UNIX.getValue());
    lineCodec.setDecoderMaxLineLength(1024 * 1024); // 1M
    lineCodec.setEncoderMaxLineLength(1024 * 1024); // 1M

    IoConnector connector = new NioSocketConnector();
    connector.getFilterChain().addLast("logger", new LoggingFilter());
    connector.getFilterChain().addLast("codec",
            new ProtocolCodecFilter(lineCodec));
    connector.setHandler(new ProviderClientHander());

// ConnectFuture connectFuture = connector.connect(new InetSocketAddress("127.0.0.1",BaseConfig.ESB_PORT));
ConnectFuture connectFuture = connector.connect(new InetSocketAddress("10.129.34.215", BaseConfig.ESB_PORT));
// 等待建立连接
connectFuture.awaitUninterruptibly();
System.out.println("连接成功");

    IoSession session = connectFuture.getSession();
    session.write(message);
    // 关闭
    if (session != null) {
        if (session.isConnected()) {
            session.getCloseFuture().awaitUninterruptibly();
        }
        connector.dispose();
    }
}

}

3个回答

yubao2008
yubao2008   2016.01.23 20:29

发给第三方服务器时日志:
信息: CLOSED
client与:/10.129.34.215:20071断开连接

而发给mina模拟的服务器商时日志:
信息: RECEIVED: HeapBuffer[pos=0 lim=1504 cap=4096: 3C 64 61 74 61 20 6E 61 6D 65 3D 22 54 54 5F 41...]
client接受信息:0000003541<?xml version="1.0" encoding="UTF-8"?>...

请问一下是怎么回事了,已困扰了好,多谢

wj01111
wj01111   2016.12.11 19:46

请问你解决了吗?我也遇到这样的问题

pizi_he
pizi_he   2017.06.09 17:54

我也是这样请问你解决了吗

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!