java - Invoke request to localhost immediately after servlet container startup -
i'm looking have java web application call @ localhost url after servlet container (be tomcat, jetty, ...) begins accepting requests.
i'm using java spring framework believe that's "side issue" since it's servlet container's status need aware of.
as understand it, spring initializes application context beans first, maps urls , initializes dispatcherservlet
handle handling/filtering of requests.
i'm looking find "moment" when can safely use resttemplate
call server itself. i've tried seems "too early" has resulted in java.net.connectexception: connection refused
--except when invoke manually web browser via controller endpoint--which succeeds.
i've tried using:
javax.servlet.servletcontextlistener
per how invoke method on servlet or controller after web container has startedorg.springframework.context.applicationlistener<contextrefreshedevent>
- a hacky custom servlet's init method "load-on-startup" after
dispatcherservlet
at point in time, servlet container must have take on spring's setup , "flip switch" "on". also, i'd in servlet container "agnostic" way don't have specific tomcat/jetty code.
here's resttemplate exception. i'm running app on port 9090 , contextpath 'openid-connect-provider'. 'foo' simple endpoint works, mentioned, when invoked after servlet container started.
org.springframework.web.client.resourceaccessexception: i/o error on request "http://localhost:9090/openid-connect-provider/foo": connection refused; nested exception java.net.connectexception: connection refused @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:582) ...
the other thing can these errors occur in logs/console before jetty tells me it's started:
[info] started serverconnector@1bcba9c7{http/1.1,[http/1.1]}{0.0.0.0:9090} [info] started @13749ms [info] started jetty server
[update] bit more background. i'm implementing oauth2 authorization server (as). user credentials in separate database need access through separate service (which oauth2 resource server or rs). need call rs authenticate users want protect rs tokens granted as. so, need setup as oauth2 client of can call rs securely. that, want dynamically register application (with itself) client_id/client_secret credentials generated can call rs. ideally, i'd want user info in same service , not have 6-month interim step.
can try little trick @ end of servletcontextlistener#contextstarted
event
new thread(() -> { try { thread.currentthread().sleep(500); // client call. } catch (interruptedexception e) { e.printstacktrace(); } }).start();
you can adjust sleep time.
Comments
Post a Comment