Frage

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
}
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top