套接字从服务器获取不完整的数据

时间:2017-08-18 16:51:50

标签: java sockets

我在weblogic中部署了java应用程序,app中的主要角色是通过套接字与另一个API进行通信。另一个API安装在另一台服务器上。确定

有时候java应用程序开始从socket获取不完整的数据。我已经分析了API方面的日志。 API返回完整数据但在部署了Java app的weblogic服务器上没有读取完整数据。本地java应用程序也可以工作。

我认为它与服务器内存有关。我不知道要解决这个问题。

我在谷歌搜索但没有答案。

在本地返回的原始邮件下面有成功:

  

@ ** @ 0 100-Autorizado o uso da NF-e 33170845242914003465652000000007511200000067 http://www4.fazenda.rj.gov.br/retrieve/QRCode?chNFe=33170845242914003465652000000007511200000067&nVersao=100&tpAmb=2&cDest=12345678998&dhEmi=323031372d30382d31385431333a30343a32302d30333a3030&vNF=110.00&vICMS=22.80&digVal=6b6e3774696b652f616b4878574a35414d766367347074497a62343d&cIdToken=000001&cHashQRCode=998144c803f3f5e56a0e1764647ccd6928c2a70d 333170000817607 | 2017-08-18T13:04:23-03:00 | SVRSnfce201707171030 | 100 http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode? 6989 * @@ *

以下是weblogic服务器中返回的消息:

  

@ ** @ 0 100-Autorizado o uso da NF-e 33170845242914003465652000000007501000000449 http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode?chNFe=33170845242914003465652000000007501000000449&nVersao=100&tpAmb=2&cDest=22233344405&dhEmi=323031372d30382d31385431323a35343a30342d30333a3030&vNF=325.00&vICMS=61.75&digVal=7955796b6b4e64687a7342335470755166495847516c4343566e453d&cIdToken=000001&cHashQRCode=03e10ca5299ed5cb5acd7de5dc5db98df9c49d0e 333170000817573 | 2017-08-18T12:54:06-03:00 | SVRSnfce201707171030 | 100 http://www4.fazenda.rj.go

以下是负责发送和读取数据的类:

@Repository

公共类SocketRepository实现了BaseRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(SocketRepository.class);

private Socket socket;
private static int BUFFER_SIZE = 2048;

private BufferedOutputStream bos;
private BufferedInputStream bis;

@Override
public synchronized String sendDataSocket(String buff, final OsbRequestDTO osbDto) throws Exception {

    try {

        LOGGER.info("start send data. ");

        URL url = new URL(osbDto.getEmissor().getUrlLoja());
        openConnection(url.getHost(), url.getPort());

        if (this.bos == null) {
            this.bos = new BufferedOutputStream(this.socket.getOutputStream(), BUFFER_SIZE);
        }

        if (this.bis == null) {
            this.bis = new BufferedInputStream(this.socket.getInputStream(), BUFFER_SIZE);
        }

        this.bos.write(buff.getBytes(StandardCharsets.UTF_8));
        this.bos.flush();

        LOGGER.info("end send data. ");
        return readMessagePaperless(bis);

    } catch (Exception e) {
        LOGGER.error("Erro :" + e.getMessage());
        throw e;
    } finally {
        LOGGER.info("start finally block. ");
        closeConnection();
        LOGGER.info("End finally block.");
    }
}

private String readMessagePaperless(final BufferedInputStream bis) throws IOException {

    try {
        LOGGER.info("start read data .");
        byte[] bufferSize = new byte[512 * 1024];
        int data = bis.read(bufferSize);
        if (data == -1) {
            return "";
        }
        LOGGER.info("end read data.");
        return new String(bufferSize, 0, data, StandardCharsets.UTF_8);
    } catch (Exception e) {
        LOGGER.error("err." + e.getMessage());
        throw e;
    }

}

private void closeConnection() throws Exception {
    try {

        if (this.bos != null && this.bis != null) {
            try {
                this.bos.close();
                this.bis.close();
            } catch (Exception e) {
                LOGGER.error("err  :" + e.getMessage());
                throw e;
            }

        }

        if (this.socket == null) {
            return;
        }
        this.socket.close();
        this.socket = null;
        this.bis = null;
        this.bos = null;

    } catch (Exception e) {
        LOGGER.error("err :" + e.getMessage());
        throw e;
    }

}

private void openConnection(final String host, final int port) throws UnknownHostException, IOException {
    LOGGER.info("start open socket conn.");

    if (this.socket != null) {
        return;
    }
    this.socket = new Socket(host, port);
    LOGGER.info("end.");
}

}

1 个答案:

答案 0 :(得分:0)

读取只会读取网络提供的一大块数据,通常大约1500字节,具体取决于各种因素。您应该继续阅读,直到收到完整的消息。当连接是本地连接时,其工作方式不同。