Correct handling of background calls in jersey - java

I have to do some background calls to a web service within a web application. The response of the call is not really time-critical and barely interesting for me. It changes only in rare cases, where I will react to it by throwing a exception, or logging the failure or something.
My conrete question now refers to the correct handling of such asynch calls in Jersey Client 2.
Option 1 uses the async calls from jersey, but then starts a thread for each response.
Option 2 would immediatly start a thread and use jersey from inside this thread.
Generally which option would be more adviseable?
Option 1
private static final ExecutorService executorService = Executors.newFixedThreadPool(20);
--
Client client = ClientBuilder.newClient();
WebTarget target = client.target("somehost").path("somepath");
final AsyncInvoker asyncInvoker = target.request().async();
final Future<Response> responseFuture = asyncInvoker.post(Entity.json(myjsonobject));
executorService.execute(new Worker(responseFuture));
-- Worker Thread
public class Worker implements Runnable {
private Future<Response> futureResponse;
public Worker(Future<Response> futureResponse){
this.futureResponse = futureResponse;
}
#Override
public void run() {
try {
final Response response = futureResponse.get();
if(response.getStatus() == 500)
doSomething();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Option 2
private static final ExecutorService executorService = Executors.newFixedThreadPool(20);
--
executorService.execute(new Worker());
-- Worker Thread
public class Worker implements Runnable {
public Worker(){
}
#Override
public void run() {
try {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("somehost").path("somepath");
ClientResponse response = target.request().accept(MediaType.APPLICATION_JSON).post(Entity.json(myjsonobject), ClientResponse.class);
if(response.getStatus() == 500) {
doSomething();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}

Instead of trying to implement a "non-blocking" Future.get() (with another thread), you can simply make use of the InvocationCallback, passing an instance to the get method. For example
Future<Response> future = target.request().async().get(new InvocationCallback<Response>(){
#Override
public void completed(Response response) {
System.out.println(response.readEntity(String.class));
response.close();
client.close();
}
#Override
public void failed(Throwable throwable) { /** Log exception **/ }
});
See more at Asynchronous Client Callbacks

Related

RESTEasy + Asynchronous + Callback Method + Java

I want to support both Synchronous and Asynchronous call using RESTEasy-JAXRS. And my asynchronous call should be based on callback, where Async request will have callbackURI, request gets processed asynchronously and upon completion makes a call to callbackURI with operation status/result. Can someone point me out to correct place? I see lot about polling model, but not callback with RESTEasy.
I am new to Asynchronous stuff...
Thanks in advance!
Thanks for your response rmlan.Yes but we have support in JAX-RS to handle asynchronous using #Suspended & AsyncResponse. I did that with following code, but i am unable to find the way to make callback to Client who called the API upon completion of task with this request.
#GET
#Path("/async")
public String checkAsync(#Suspended final AsyncResponse response) {
response.setTimeoutHandler(new TimeoutHandler() {
#Override
public void handleTimeout(AsyncResponse asyncResponse) {
response.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
.entity("Operation time out.").build());
}
});
response.setTimeout(2, TimeUnit.SECONDS);
new Thread(new Runnable() {
#Override
public void run() {
String result = veryExpensiveOperation();
response.resume(result);
}
private String veryExpensiveOperation() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.debug("Task is processed fully");
return "Successful";
}
}).start();
return "nothing";
}
Every method in class has one return ( or maybe void ) . but sometimes you need some callbacks from your method . this is the solution .
public abstract class MyClass {
public abstract void myCallbackMethod();
public void myMethod(){
for (int i = 0; i < 5; i++) {
// do somthing
myCallbackMethod();// it will implements in future.
}
}
}
when you make an object from MyClass , you must implement myCallbackMethod abstract method . like this
public class NewMain {
public static void main(String[] args) {
MyClass myClass=new MyClass() {
#Override
public void myCallbackMethod() {
System.err.println("this is call back");
}
};
myClass.myMethod();
}
}
and the result is
this is call back
this is call back
this is call back
this is call back
this is call back
it means you can get five call backs from void method in your class.
it is a good way when you do not have any idea for the body content of myCallBackMethod
the real example is download a file from server in your app .
you can call myCallBackMethod when bytes received and progress your progressbar

Multithreaded Java worker with a size restricted resource pool

I have this 'Worker' class, which uses a resource 'Client'.
There may be any number of threads, running the 'Worker' at any given time.
The 'Client' is not thread-safe, thus I'm using 'ThreadLocal' for it.
The 'Client' connects to some server and executes a HTTP 'Request' that the worker feeds the 'Client'.
public class Worker {
// Client is NOT thread-safe !!!
private static ThreadLocal<Client> client = new ThreadLocal<Client>();
#Override
protected void onGet(Request req) {
handleRequest(req);
}
private void handleRequest(Request req) {
someRunnableExecutor(new Runnable() {
#Override
public void run() {
get_client().send_req(req);
}
});
}
private Client get_client() {
Client c = client.get();
if (c == null) {
c = new Client();
client.set(c);
}
return c;
}
At the current implementation (above), stripped down for clarity, there are as many "active" 'Clients' as there are running 'Workers'.
This is a problem because the server is being exhausted.
What I can do is only fix the 'Worker'. Have no access to the 'Client', server or the executor that runs the workers.
What I want to do is to have a Queue of 'Client'(s) and a piece of a synchronized code, in the 'Worker', that takes a 'Client' off the Queue, if the Queue is empty the 'Worker' should wait till there is one in the Queue for him to take. Then put the 'Client' back into the Queue - synchronized as well.
I really want to keep it as simple as possible, with the possible minimum changes made to the code.
No new classes, no factories, just some data structure to hold the 'Client'(s) and synchronization.
I am a bit puzzled with how to achieve that generally, as well as by the fact that the 'Client' is not thread-safe and that I have to 'ThreadLocal'(ize) it. Is this how do I put that in a Queue?
private static Queue<ThreadLocal<CLient>> queue =
new LinkedList<ThreadLocal<CLient>>();
Also, how/where do I initialize that Queue, once, with say 5 clients?
Please share your thoughts.
You don't need ThreadLocal here, as you want to have less Clients than Workers. All you need in BlockingQueue.
Notice! I supposed that Client's send_req is synchronous, if it's not - the code needs some changes in run() method
public class Worker {
private static final int CLIENTS_NUMBER = 5;
private static final BlockingQueue<Client> queue = new LinkedBlockingQueue<>(CLIENTS_NUMBER);
static {
for (int i = 0; i < CLIENTS_NUMBER; i++)
queue.put(new Client());
}
#Override
protected void onGet(Request req) {
handleRequest(req);
}
private void handleRequest(Request req) {
someRunnableExecutor(new Runnable() {
#Override
public void run() {
try {
Client client = takeClient();
client.send_req(req);
putClient(client);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
}
private Client takeClient() throws InterruptedException {
return queue.take();
}
private void putClient(Client client) throws InterruptedException {
queue.put(client);
}
}

Why does matlabcontrol interrupt calling thread upon proxy create?

I made my Matlab controlling thread interruptable and found, that it is interrupted all the time on first run.
This is because GetProxyRequestCallback has interrupting code inside:
private static class GetProxyRequestCallback implements RequestCallback
{
private final Thread _requestingThread;
private volatile MatlabProxy _proxy;
public GetProxyRequestCallback()
{
_requestingThread = Thread.currentThread();
}
#Override
public void proxyCreated(MatlabProxy proxy)
{
_proxy = proxy;
_requestingThread.interrupt();
}
public MatlabProxy getProxy()
{
return _proxy;
}
}
Are there any reasons to interrupt calling thread or this is just a bug?
The RemoteMatlabProxyFactory.getProxy() method creates an instance of GetProxyRequestCallback and then sleeps, waiting for the proxyCreated(...) method to be called. Therefore, if proxyCreated() did not interrupt the thread that originally created the request, this thread would wait until the timeout would be reached.
In my opinion, this is a flaw in the matlabcontrol library: Thread.interrupt() should not be abused for this purpose because a thread being interrupted can have different reasons and should not be used for anything except signaling that the thread should stop.
This should be fixed in the matlabcontrol library by waiting on a mutex instead.
For example:
class RemoteMatlabProxyFactory implements ProxyFactory {
// [...]
#Override
public MatlabProxy getProxy() throws MatlabConnectionException {
GetProxyRequestCallback callback = new GetProxyRequestCallback();
Request request = this.requestProxy(callback);
return callback.getProxy(_options.getProxyTimeout());
}
// [...]
}
private static class GetProxyRequestCallback implements RequestCallback {
private final Object _lock = new Object();
private MatlabProxy _proxy;
#Override
public void proxyCreated(MatlabProxy proxy) {
_proxy = proxy;
_requestingThread.interrupt();
}
public MatlabProxy getProxy(long timeout) throws MatlabConnectionException {
synchronized (_lock) {
if (_proxy != null) {
return _proxy;
}
try {
_lock.wait(timeout);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MatlabConnectionException("Thread was interrupted while waiting for MATLAB proxy", e);
}
if (_proxy == null) {
throw new MatlabConnectionException("MATLAB proxy could not be created in " + timeout + " milliseconds");
}
return _proxy;
}
}
}

How can I start a server in a background thread and know that the server did not throw exception on startup?

I have a class which encapsulates a Server Socket i.e. a server functionality.
The interface of the class is:
class Server{
public void start();
public void stop();
}
the start is as follows:
public void start(){
ExecutorService tp = Executors.newSingleThreadExecutor();
while(!stop){
try {
Socket clientConnection = serverSocket.accept();
tp.execute(new ClientProcessor(clientConnection));
} catch (IOException e) {
stop = true;
}
}
I have trouble figuring out how I would start this without blocing my main.
I mean I need to start the server from a background thread so I thought of the following:
ExecutorService tp2 = Executors.newSingleThreadExecutor();
tp.submit(new Runnable(){
public void run(){
Server s = new Server();
s.start();
}
});
}
But what I want is to know that the server started and did not throw an exception. How can I do that?
I.e. how can I know from my main thread that the background server started fine and so I can continue with the rest actions?
Start the server.
Wait a bit.
Try to connect to it on the local stack. If the connect succeeds, you have a winner - just close the temp connection.
Here is a very simple example (though there are many better - and more complicated - ways):
interface Server implements Thread{
public void startup();
public void shutdown();
}
class MyServer implements Server{
private Exception serverError=null;
ExecutorService singleThreadManager;
public Exception getServerError(){
return serverError;
}
public void run(){
while(!stop){
try {
Socket clientConnection = serverSocket.accept();
singleThreadManager.execute(new ClientProcessor(clientConnection));
} catch (IOException e) {
serverError = e;
// stop = true;
shutdown(); // shutdown cleanly after exception
}
}
}
public void startup(){
singleThreadManager = Executors.newSingleThreadExecutor();
// do setup tasks and then
start(); // thread.run() will now be called asynchronously
}
}
public static void main (String ... args){
MyServer s = new MyServer();
s.startup();
// whenever you want you can now call
Exception x = s.getServerError();
// to see whether the server has shutdown because of an error.
}
maybe you can implement an Event Handler into your main application. Your main application should be registered as eventlistener to your server class.
Please have a look at the following link, it shows you an example of event handling for C# and the same example for Java
http://scatteredcode.wordpress.com/2011/11/24/from-c-to-java-events/
A simple way of doing this would be to collect all exceptions in a collection and return them to the foreground thread at some point in time.
class Server{
private Collection<? extends Exception> exceptions = new ArrayList<Exception>();
public void start()
{
try{ /* do something*/ }
catch(Exception e)
{
exceptions.add(e)
}
}
public void stop();
public Collection<Exception> getAllExceptions()
{
return exceptions;
}
public boolean checkOk()
{
return 0 == exceptions.size();
}
}
Elaboration on how to transfer data between threads:
If you declared your server similarly to my suggestion above, then we can change the code you have used to spawn your server to transfer the serverStartupOk information:
ExecutorService tp2 = Executors.newSingleThreadExecutor();
final boolean[] result = new boolean[1]; //Final object so it can pass between threads
tp.submit(new Runnable(){
public void run(){
Server s = new Server();
s.start();
result[0] = s.checkOk(); // Retrieve the check from the server, store it in
// final array variable
}
});
boolean serverStartupOk = result[0];
System.out.println("Server startup succesful: " + Boolean(serverStartupOk).toString());
}
Use your own ThreadPoolExecutor instead of the pre-baked ones in Executors. Then override the afterExecute hook provided by TPE to do whatever you want with the exceptions.
I'm reading a bit between the lines here, but it looks like you want to know if the initial listen on the socket succeeded or not. If you wanted to wait and see if any client failed, then there would be no need to run it in a different thread.
If that's correct, then you can catch any exceptions emitted by the initial TCP server socket instantiation before you start the thread to handle client connections. Pass the server in to the new thread, instead of creating it in the new thread.

Active Object Pattern in Concurrent Java 1.5+

I am trying to develop active object pattern in concurrent Java using java.util.concurrent classes.
I describe it using a Client and a Server. A sample Server is as:
class Server implements Runnable {
public final LinkedBlockingQueue que = new LinkedBlockingQueue();
private final ExecutorService es = Executors.newCachedThreadPool();
private Message currentMessage;
private boolean state = false;
public init() {
es.submit(this);
}
public void requestForServer() {
if (state) {
this.currentMessage.await();
}
state = true;
}
public void run() {
for(;;) {
Message m = que.take();
this.currentMessage = m;
this.es.submit(m);
}
}
}
And a sample Client:
class Client {
private Server server;
public Client(Server s) {
this.server = s;
}
public void doSomething() {
Message m = new Message(new Callable() {
public Object call() {
server.requestForServer();
}
});
this.server.que.add(m);
}
}
And a sample Message encapsulation is:
class Message<V> extends FutureTask<V> {
private Lock lock = new ReentrantLock();
private Condition condition = new Condition();
public Message(Callable<V> callable) {
super(callable);
}
public void run() {
try {
lock.lock();
super.run();
lock.unlock();
} catch(Exception e) {}
}
public void await() {
try {
condition.await();
} catch(Exception e) {}
}
public void signal() {
try {
condition.signalAll();
} catch(Exception e) {}
}
}
And a sample running code:
Server s = new Server();
Client c = new Client (s);
s.init();
c.doSomething();
I dropped some implementation details to get my message across.
Now, the problem is when in Server the state is true so the incoming message should wait and the await is called on the current message. However, I get IllegalMonitorStateException which means that the current message does not own the current thread to await on it. But, I believe this is strange since the current message gets called in the Server and its thread pool so the current message has also an access to the current thread of execution.
I'd be most thankful for any ideas or suggestions, or with a known working implementation of this pattern using java.util.concurrent. Thanks in advance.
UPDATE:
I discussed the solution I could deploy in this blog post. I hope it could help.
You have to actually acquire the lock when you await on its corresponding condition. Without that lock you cannot associate yourself to the condition directly. To demonstrate this:
public void await() {
lock.lock();
try {
condition.await();
} catch(Exception e) {}
finally{
lock.unlock();
}
}
That should resolve your IllegalMonitorStateException
On a side note of correctness you should always release a lock in a try{ } finally{ } manner, you can observe what I wrote as an example. The reason for this is if an exception occurs between lock().lock(); and super.run(); lock.unlock() will never be called.

Categories