0

To Start: I am a novice in opensource software. (Apache-Tomcat / Java / Restletframework)

Here is my question: I am building a application with the Restlet framework. I do not know if my coding/methody is Thread-safe!? Can somebody tell me if I am coding the/a right way? Or am I hopelessly failing?

Construct:

  • CLASS A will be called by a client. (the REST-request)
  • CLASS A (the router) will call CLASS B
  • CLASS B is my Central request handler, this CLASS B calls another CLASS C
  • CLASS C is the actual service that was requested, in this example the login-service -

As you can see the login subclass is static. Is this a thread-safe construction?

Regards

CLASS A

public class MyStartApplication extends Application {


//Creates a root Restlet that will receive all incoming calls.

@Override
//public synchronized Restlet createInboundRoot() {    //synchronized?
public  Restlet createInboundRoot() {


    //Create a router that routes each call to a new instance of a Resource.
    Router router = new Router(getContext());

    // First we use MODE_START_WITH to determine the requested destination
    // A TRAPDOOR for all requests for this TEST
    // We reroute it to THE CENTRAL RESTLET-WRAPPER 
TemplateRoute route = router.attach("/testmywrapper/", RestletWrapper.class);
    route.getTemplate().setMatchingMode(Template.MODE_STARTS_WITH);


    // Return the response to caller 
    return router;


}

}

CLASS B

public class RestletWrapper extends ServerResource {

@Get
public JSONObject start()    {

    JSONObject returnObj = null;

    switch(operation){
    case "login":
        returnObj= LoginUser.login(queryparams);
        break;
    }

    Return returnObj
}
}

CLASS C

public class LoginUser {


public static JSONObject login(JSONObject queryparams) throws Exception {
    do some stuff
    return object
}
}

1 Answers1

0

Thread safety can be a problem if several threads access some shared state.

Unless the code hidden behind "do some stuff" uses static fields or singletons, there should be no thread-safety problem: all your variables are local to the login method, and thus not shared between threads.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • so the matter that the subclass is defined "static" is not thread-unsafe perse? – user1887511 Dec 08 '12 at 11:03
  • and also I was wondering about the "sychronized" keyword in the first Restlet-Starter-Application, I read that it is removed from the subclass createInboundRoot() in the new Restlet-version..., MUST I use that keyword in the older Restlet versions, And/Or does that not influence thread-safety? – user1887511 Dec 08 '12 at 11:08
  • I don't know about restlet. Read the manual. Also, you don't have any "static subclass". You have a static method. A method, static or not, that only uses local variables is inherently thread-safe. If that's the case of the login method, and if the queryparams object is not shared between threads (and it looks like it isn't shared between threads), then this method is thread-safe. – JB Nizet Dec 08 '12 at 11:17
  • OK, Thanks JB, cleared that up makes me feel a bit more secure on continuing my quest. – user1887511 Dec 08 '12 at 11:26