SocketTimeOut Exception in java whiile calling RestTemplate GetforObject

大城市里の小女人 提交于 2020-01-24 00:34:12

问题


we experience the following error pattern:

I/O error on GET request for "https://samplepath": Timeout while fetching URL: https://samplepath; nested exception is java.net.SocketTimeoutException: Timeout while fetching URL: samplepath

Code:

    RestTemplate restTemplate = new RestTemplate();
    try {
        URI uri = new URI("https://samplepath);
        sessionInfo = restTemplate.getForObject(uri, SessionResponse.class);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

This code is written inside the javax.servlet.Filter where all the reuests will migrate to this block of code where in this it will internally call another api

Issue is after i deploy in GAE concurrent request get hits and some of the requests will get response and some are failing or throwing socket timeout exception by doing internal one Api call through resttemplate

Any solution to this issue will be appreciated thanks,,

@Component
public class FirstFilter implements Filter {
    public static Map<String, SessionResponse> sesssionDataCacheMap = Collections
            .synchronizedMap(new HashMap<String, SessionResponse>());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init() method has been get invoked");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        try {
            System.out.println("doFilter() method is invoked");
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            System.out.println("Context path is  " + httpServletRequest.getContextPath());

            String sidFromHeader = httpServletRequest.getHeader("sid");

            SessionResponse sessionInfo1 = null;
                        try {
                            sessionInfo1 = getSessionDataFromSessionId(sidFromHeader);
                            System.out.println("Session  present in cache");
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        chain.doFilter(httpServletRequest, httpServletResponse);
                        System.out.println("doFilter() method is ended");
            }
    }
    @Override
    public void destroy() {
    System.out.println("destroy method is called");
    }

    private SessionResponse getSessionDataFromSessionId(String sessionId)  {

        SessionResponse sessionInfo = null;
        RestTemplate restTemplate = new RestTemplate();
        try {
            URI uri = new URI("https://samplesessionurl" + sessionId);
            sessionInfo = restTemplate.getForObject(uri, SessionResponse.class);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sessionInfo;

    }

Appegine-web.xml File--

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
        <threadsafe>true</threadsafe>
        <runtime>java8</runtime>
        <system-properties>
            <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
        </system-properties>
        <url-stream-handler>urlfetch</url-stream-handler>
        <warmup-requests-enabled>false</warmup-requests-enabled>
        <manual-scaling>
        <instances>1</instances>
      </manual-scaling>
    </appengine-web-app>

来源:https://stackoverflow.com/questions/59775177/sockettimeout-exception-in-java-whiile-calling-resttemplate-getforobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!