Frequently Asked Questions |
Contents
- When should I use synchronous vs asynchronous calls?
- How do I access the HttpSession, HTTPServletRequest or other server side objects from within a method call?
- Can jabsorb work without using HttpSession?
- Does jabsorb support clustering?
- Does jabsorb require the use of JSP?
- How do I create a session bridge from a servlet?
- In JavaScript code, how do I access variables related to a specific RPC call from within its async callback function?
- How do I enable debugging?
1. When should I use synchronous vs asynchronous calls?
Always use asynchronous calls.
You should almost never use synchronous calls. Due to implementation problems with the XMLHttpRequest object, synchronous calls effectively hang the browser. If there are large network or application delays, this can appear similar to the browser crashing.
2. How do I access the HttpSession, HTTPServletRequest or other server side objects from within a method call?
Use Local Argument Resolvers. This may sound like a fancy thing, but it's actually extremely simple.
For example, if you have a method
public void doSomething(int arg1, int arg2)
{
// do something
}
change the method to:
public void doSomething(HttpSession session, int arg1, int arg2)
{
// do something -- now you can use the session!
}
In the above example, you would call the method from JavaScript in the exact same way.
The jabsorb framework automagically detects several different types of arguments in any method call, and fills them in with the correct values from the servlet and/or jabsorb framework and it omits these extra arguments from the runtime JavaScript signature of the method call. By default, the following class types can be automatically filled in:
HttpServletRequest
HttpServletResponse
HttpSession
- JSONRPCBridge
You can include any number of these arguments, and in any order (although including the same type more than once wouldn't make much sense) and they will all be invisible from the client point of view.
If you really do want to get fancy, you can create your own local argument resolvers. See the manual for more information on Local Argument Resolvers.
3. Can jabsorb work without using HttpSession?
Yes, you can do so by exporting your classes and objects in the global bridge. The global bridge singleton is accessed by JSONRPCBridge.getGlobalBridge(). Classes and objects are exported to all clients when registered with the global bridge.
jabsorb does not by default create a JSONRPCBridge instance in the session. You must do this yourself with a scope="session" usebean clause in JSP or associated Java code if using servlets. This is so that jabsorb can be used without the overhead associated with sessions.
4. Does jabsorb support clustering?
Yes, if you register objects or classes in a JSONRPCBridge instance in the session, this information can be clustered if your container supports it. All data stored by jabsorb in the session is Serializable.
Note: If you register your own classes as References, Callable References, or Callbacks, against a session bridge instance, then your classes need to implement Serializable aswell.
5. Does jabsorb require the use of JSP?
No, it does not. It just happens that the demo code included with the distribution uses JSP. You can use jabsorb with any web framework.
6. How do I create a session bridge from a servlet?
You just construct and place an JSONRPCBridge instance in the session under the key "JSONRPCBridge". See the tutorial example "Using the JSONRPCBridge in a Servlet" for example code.
7. In JavaScript code, how do I access variables related to a specific RPC call from within its async callback function?
To achieve this in the JavaScript way, you can take advantage of closures.
When a function is defined within a function in JavaScript, this creates a closure, where all variables in the scope of the outer function are available in the defined function when it is called later.
An example:
function selectUser(username)
{
// some data we want available in scope of callback
var row = lookupRow(username);
var cb = function(result, ex) {
if(ex) throw ex;
// The row and username variables are visible
// here later when this callback function is called
updateRow(row, username, result);
};
jsonserver.userDb.fetchUser(cb, username);
}
8. How do I enable debugging?
jabsorb uses SLF4J to log all debugging messages.
Enabling debugging is dependant on the particular log backend you are using. See the SLF4J Documentation for more details.
jabsorb logs debug messages at the 'debug' level. This may not be enabled by default with normal logging configurations (and should normally be disabled on production systems due to the increased load).
If you are using the bundled slf4j-jdk backend, then you can enable debug messages by adding the following to your logging.properties file.
org.jabsorb.level = FINE
You can usually find logging.properties in the JAVA_HOME/jre/lib/ directory.