How to set timeout for Jersey REST web services? - java

I would like to know how to set a timeout for a REST web service with Jersey, and being able to catch it within the service. I've read some approaches to achieve this, such as calling another service after the timeout to check if the current service is active, or verifying application credentials, etc.
I'd rather not to follow these approaches. In fact, I would like to know if is possible to set a listener to the HTTP request, or to the service itself, that would execute some procedure if
the timeout is reached.
I suppose that creating a thread within the service body to act as listener could be a solution, but I'd like to know if there is a solution closer to Jersey.

I'm pretty sure that Jersey like many other api's has client timeout functionality built in.
Don't want to give you loads of code so you can check out these posts
Not sure I remember correctly but I think you can set it using this clint api through setReadTimeout and setConnectTimeout*
https://jersey.java.net/apidocs/1.1.1-ea/jersey/com/sun/jersey/api/client/Client.html

Related

Can I put an HTTP proxy service in my java app to intercept all HTTP requests and change the headers?

I am writing an android app in java, and would like to intercept all HTTP traffic from its outgoing requests and change the user agent from the header. I know there are tools to do this from separate apps/programs like Charles' Proxy, but can I do this from within the app itself? Would I be able to register a service in my app for this? If so, do you have any links I can reference to get started or have any tips? I am a novice coder, so anything at all is appreciated. Thank you.
Do you use OkHttp to make requests? If so, the library has a powerful mechanism of interceptors to do exactly what you want.

Using Java, how to determine the outbound service calls from a tomcat webserver?

Basically, I want to determine/log all the outbound service calls happening from our Spring application for a given request.
I would like to figure out all the REST/HTTP calls being made from our application.
What is the best way to achieve this?
Configure the JVM to use a proxy, and then completely block outgoing connections that don't go to the proxy. Then, (wait for it), watch everything that goes through the proxy.

How to handle an asynchronous response in a java library?

I am developing a library which sends an asynchronous request to a server (in another thread).
Our clients will use this library, and I am thinking of the best way how to get this handled by them. The response might follow in seconds to a minute. I am doing this using the jersey-client and there already exists a listener which will be called in the other "async" thread.
So I have a shared ressource which will be "filled" with the response by the listener, but the main-thread has to call it to retrieve the response.
Is there a better way how the client might access or be "notified" that the response has arrived? Something like an EventNotifier? (If i implement something like this, shouldn't there exist something like polling on the notifier, but this means that there might exist another thread, which has to do this).
I have no influence on the client who is using my library, I can only make their life easier with an appropriate design of the library-functions they call.
What is the servlet container version you are trying to use ? Servlet 3.0 has builtin support for it

How a HTTP server respondes to a client's request

could you please give me a sample code on how an Http Server(Java) receives the request of a client(android)? I mean the client sends the request via Httppost, how the server takes the content of these requests in order to see the context and reply? I am trying to built a chat application.
Thank you in advance!
The server-side of HTTP is usually implemented using the protocol stack provided by a web container. You would then implement your application's server-side as servlets. There are numerous tutorials on this.
If that's the way you want to proceed, look at one of the standard web containers; e.g. Tomcat, Jetty, Glassfish, etc. The source code for all of these is available for you to browse, though I should warn you that they are all complicated under the hood.
Assuming that your HTTP service is going to be delivering JSON or XML (rather than HTML) to clients, you may want to look into using a RESTful framework.
Have a look at ServerSocket. Keep in mind that accept() blocks and, as you will probably run it in a service, you will want to time it out and check for the completion of the service. That should probably run in its own thread as should the responders to requests.
From there, you can open input and output streams to receive the request and write the response. There are any number of packages that can help you with the interaction, or you can roll your own, but it doesn't seem like you've done a lot of homework. Perhaps some searching, reading, and more specific questions would more you along more quickly.

Asynchronous web services problem

i need one answer. I am calling Asynchronous web services and ofcourse it want Asynchronous response. Must webservice somehow call my client or how is implemented response.
I read this and many other pages but i do not know if web service need to call me back for response.
http://www.mastertheboss.com/web-interfaces/111-asynchronous-web-services-with-jboss-ws.html
regards
You need to think it as a 3rd party service which you dont have any access to its source.
You need to handle asyn calls yourself for your side.
java.util.concurrent package contains what you want
see below link
http://www.scribd.com/doc/17369778/Java-Concurrent-Package-tutorial

Categories

Resources