Skip to content

Commit

Permalink
sugestão de forma simples de sobrescrever o timeout das requisições (#…
Browse files Browse the repository at this point in the history
…691)

Co-authored-by: Leandro Del Sole <[email protected]>
Co-authored-by: Leandro Del Sole <[email protected]>
Co-authored-by: Diego Fincatto <[email protected]>
  • Loading branch information
4 people authored Sep 21, 2020
1 parent 8c8ab4a commit e177345
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/fincatto/documentofiscal/DFConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fincatto.documentofiscal;

import com.fincatto.documentofiscal.utils.DFPersister;
import com.fincatto.documentofiscal.utils.DFSocketFactory;

import org.simpleframework.xml.core.Persister;

import java.security.KeyStore;
Expand Down Expand Up @@ -94,6 +96,16 @@ public String getCertificadoAlias() {
public String[] getSSLProtocolos() {
return new String[]{"TLSv1.2"};
}

/**
* Timeout a ser utilizado em cada requisição feita ao SEFAZ de origem.
* Deve ser maior que zero, se não, será utilizado {@link DFSocketFactory#TIMEOUT_PADRAO_EM_MILLIS}
*
* @return timeout da requisição em millisegundos
*/
public int getTimeoutRequisicaoEmMillis() {
return DFSocketFactory.TIMEOUT_PADRAO_EM_MILLIS;
}

/**
* Retorna o timezone a ser usado no sistema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.security.UnrecoverableKeyException;

public class DFSocketFactory implements ProtocolSocketFactory {


public static final int TIMEOUT_PADRAO_EM_MILLIS = 60_000;

private final DFConfig config;
private final SSLContext sslContext;

Expand All @@ -34,10 +36,12 @@ public Socket createSocket(final String host, final int port, final InetAddress
((SSLSocket) socket).setEnabledProtocols(this.config.getSSLProtocolos());
socket.bind(new InetSocketAddress(localAddress, localPort));

int connectTimeout = 60000;
int connectTimeout = TIMEOUT_PADRAO_EM_MILLIS;

if(params != null && params.getConnectionTimeout() > 0) {
if (params != null && params.getConnectionTimeout() > 0 && params.getConnectionTimeout() != connectTimeout) {
connectTimeout = params.getConnectionTimeout();
} else if (config.getTimeoutRequisicaoEmMillis() > 0 && config.getTimeoutRequisicaoEmMillis() != connectTimeout) {
connectTimeout = config.getTimeoutRequisicaoEmMillis();
}

socket.connect(new InetSocketAddress(host, port), connectTimeout);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/fincatto/documentofiscal/DFConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void testaDefaults() {
Assert.assertEquals(DFModelo.NFE, config.getModelo());
Assert.assertEquals("TLSv1.2", config.getSSLProtocolos()[0]);
Assert.assertNotNull(config.getTimeZone());
Assert.assertEquals(60000, config.getTimeoutRequisicaoEmMillis());
}

@Test
Expand All @@ -38,6 +39,17 @@ public TimeZone getTimeZone() {
};
Assert.assertEquals(TimeZone.getTimeZone("America/New_York"), config.getTimeZone());
}

@Test
public void testaTimeoutRequisicaoEmMillis() {
final DFConfig config = new DFConfigTeste() {
@Override
public int getTimeoutRequisicaoEmMillis() {
return 30_000;
}
};
Assert.assertEquals(30_000, config.getTimeoutRequisicaoEmMillis());
}

private static class DFConfigTeste extends DFConfig {

Expand Down

0 comments on commit e177345

Please sign in to comment.