10

I have a strange problem here.

The below code is working fine until I restart the tomcat server at client side. Once I restart the tomcat server (client program is there in the war file) with the latest war file of the same code, it throws the below error. I am using JDK 8.

Below is the sample code. From the browser, I am able to get a response from the URL used in the below program. but not able to get the data using java program or Postman also.

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class TestService implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        download();
    }
    
    private void download() {
        System.out.println("Started download");
        try {
            RestTemplate restTemplate = new RestTemplate();
            String url = "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json";
            byte[] forObject = restTemplate.getForObject(url, byte [].class);
            System.out.println(forObject);
            System.out.println("Downloaded");
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
    }
}

Here's the exception raised:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json": Connection reset; nested exception is java.net.SocketException: Connection reset
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:345)
    at com.sudhasoft.service.impl.StocksServiceImpl.getNiftyData(StocksServiceImpl.java:183)
    at com.sudhasoft.service.impl.StocksServiceImpl.getNifty500Data(StocksServiceImpl.java:154)
    at com.sudhasoft.service.impl.PatternServiceImpl.getDataBySignal(PatternServiceImpl.java:444)
    at com.sudhasoft.service.impl.PatternServiceImpl.loadDataOnInit(PatternServiceImpl.java:1090)
    at com.sudhasoft.service.CacheServiceImpl.initCache(CacheServiceImpl.java:29)
    at com.sudhasoft.scheduler.job.CacheJob.clearCache(CacheJob.java:41)
    at com.sudhasoft.scheduler.job.CacheJob.executeJob(CacheJob.java:25)
    at com.sudhasoft.scheduler.StockScheduler$1.run(StockScheduler.java:120)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at okio.Okio$2.read(Okio.java:139)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
    at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at org.springframework.http.client.OkHttp3ClientHttpRequest.executeInternal(OkHttp3ClientHttpRequest.java:73)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)
    ... 10 common frames omitted
5
  • 2
    Provided we could ignore that this problem occurred only after restarting the server (or client?), this would be a duplicate of stackoverflow.com/questions/5507878/ssl-connection-reset or similar stackoverflow.com/questions/37005352/…, i. e. client and server use incompatible SSL setup... Commented Dec 10, 2019 at 10:28
  • Looks like your actual code is different from the sample code provided here. Without looking into the actual code, it's not possible to find the root cause. Commented Dec 12, 2019 at 6:56
  • Did you try skipping SSL verification Commented Dec 13, 2019 at 11:34
  • Could you please post your POM.XML file Commented Dec 16, 2019 at 13:34
  • What do you mean by ‘Tomcat server at the client side’? Commented Dec 9, 2023 at 23:21

3 Answers 3

1

The simplest answer is when you restart tomcat simply you close the socket and then you try to send more data to the closed socket. that is why you get IO exception since you can't write data

Sign up to request clarification or add additional context in comments.

Comments

-1

This seems to be due to a firewall issue.

In order to solve this issue, you will need to use Proxy Server. You can add proxy server and port using following 2 lines of code:

System.setProperty("proxyHost", "proxyServer.proxy.com");
System.setProperty("proxyPort", "9801");

1 Comment

This is a breakage of an existing connection. If it was a firewall problem there would be no connection to break.
-1

This looks like proxy related error.

You need to provide proxy information in order to connect over that URL.

Its working in browser because an internet proxy might already be configured there, it needs to be explicitly provided at code level in order to connect over internet.

Same is the case with Postman. You have to provide proxy server information under Postman configuration.

1 Comment

This is a breakage of an existing connection. If it was a proxy problem there would be no connection to break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.