Restlet client resource 'put' and threading

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Restlet client resource 'put' and threading

Frank Kolnick
I have a simple Java app that uses Restlet to send messages to a remote server. A thread running in the background has these lines of code:

ClientResource resourceHB = new ClientResource ( uri );    
resourceHB.put ( "heartbeat" );    

which works fine. I.e., the message is sent to the desired server.

However, I have copied the same lines to another thread (but using a different URI). Two things happened:

    at run time, I get a message that the protocol isn't specified, so, after some research, I changed the code to:

    Client client = new Client ( new Context(), Protocol.HTTP );
    ClientResource resourceFault = new ClientResource ( uri );
    resourceFault.setNext ( client );
    resourceFault.put ( "error" );

    that eliminates the protocol error, but the 'put' gets a 'bad request' HTTP error; the difference seems to be that in the first case, the HTTP message is sent automatically as 'octet_stream' (which is what the remote server expects) while in the second it is defaulting to 'html'.

My questions are:

    why do I have to add the extra code in the second case?

    how do I force the content type to be 'octet_stream'? I've tried:

    resourceFault.put ( "error", MediaType.APPLICATION_OCTET_STREAM );

    but Fiddler still shows the header with 'text/html'.

    again, why does the second case need so much extra information? -- the first case 'just works'

    is it not possible to have multiple client resources, particularly in different threads?

Java 1.6
Restlet version 2.2.1 for Java SE
org.restlet.jar (no extensions)

The main program uses:

import org.restlet.resource.ServerResource;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;

The thread containing the first snippet uses:

import org.restlet.resource.ClientResource;

The thread containing the second snippet uses:

import org.restlet.resource.ClientResource;
import org.restlet.data.Protocol;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.representation.StringRepresentation;
import org.restlet.data.MediaType;

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3084823
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Restlet client resource 'put' and threading

Thierry Boileau-4

Hello Franck,

thanks for the detailled data.
I've failed to reproduce your issue. I send you my sample eclipse project. It contains a SeverApplication class (Restlet-based server) and a client test class that starts two threads calling the server.
As I see no issue, could you complete this code or provide a reproductible test code?

Best regards,
Thierry Boileau
nb : here is the related StackOverFlow question (http://stackoverflow.com/questions/24481815/restlet-client-resource-put-and-threading/24507602)


2014-07-09 16:05 GMT+02:00 Frank Kolnick <[hidden email]>:
I have a simple Java app that uses Restlet to send messages to a remote server. A thread running in the background has these lines of code:

ClientResource resourceHB = new ClientResource ( uri );
resourceHB.put ( "heartbeat" );

which works fine. I.e., the message is sent to the desired server.

However, I have copied the same lines to another thread (but using a different URI). Two things happened:

    at run time, I get a message that the protocol isn't specified, so, after some research, I changed the code to:

    Client client = new Client ( new Context(), Protocol.HTTP );
    ClientResource resourceFault = new ClientResource ( uri );
    resourceFault.setNext ( client );
    resourceFault.put ( "error" );

    that eliminates the protocol error, but the 'put' gets a 'bad request' HTTP error; the difference seems to be that in the first case, the HTTP message is sent automatically as 'octet_stream' (which is what the remote server expects) while in the second it is defaulting to 'html'.

My questions are:

    why do I have to add the extra code in the second case?

    how do I force the content type to be 'octet_stream'? I've tried:

    resourceFault.put ( "error", MediaType.APPLICATION_OCTET_STREAM );

    but Fiddler still shows the header with 'text/html'.

    again, why does the second case need so much extra information? -- the first case 'just works'

    is it not possible to have multiple client resources, particularly in different threads?

Java 1.6
Restlet version 2.2.1 for Java SE
org.restlet.jar (no extensions)

The main program uses:

import org.restlet.resource.ServerResource;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;

The thread containing the first snippet uses:

import org.restlet.resource.ClientResource;

The thread containing the second snippet uses:

import org.restlet.resource.ClientResource;
import org.restlet.data.Protocol;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.representation.StringRepresentation;
import org.restlet.data.MediaType;

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3084823


testFranck.zip (10K) Download Attachment
Loading...