JBoss.orgCommunity Documentation
The Resource Adaptor Type is the interface which defines the contract between the RA implementations, the SLEE container, and the Applications running in it.
The name of the RA Type is HttpClientResourceAdaptorType
, its vendor is org.mobicents
and its version is 4.0
.
The single activity object for HTTP Client Resource Adaptor is the net.java.client.slee.resource.http.HttpClientActivity
interface. Through the activity an SBB can send multiple HTTP requests, and receive the related responses asynchronously. Due to the nature of SLEE activities, this RA activity acts like a queue of requests, allowing the processing of their responses - the events- in a serialized way
An activity starts on demand by an SBB, through the RA SBB Interface, and it ends when the response is received, or when the SBB invokes its endActivity()
method.
The HttpClientActivity
interface is defined as follows:
package net.java.client.slee.resource.http;
import org.apache.commons.httpclient.HttpMethod;
public interface HttpClientActivity {
public String getSessionId();
public void endActivity();
public boolean getEndOnReceivingResponse();
public HttpContext getHttpContext();
public void execute(HttpUriRequest request,
Object requestApplicationData);
public void execute(HttpHost target,
HttpRequest request,
Object requestApplicationData);
}
getSessionId()
method:
Retrieves the activity unique Id.
getEndOnReceivingResponse()
method:
Returns true if this Activity is set to end as soon as the Response is received.
getHttpContext()
method:
Retrieves the http context used to send requests.
execute(HttpUriRequest, Object)
method:
Executes the specified HTTP request asynchronously. The application may optionally provide a data object, to be returned in the HTTP response event, for instance an object which uniquely identifies the request sent.
execute(HttpHost, HttpRequest, Object)
method:
Executes the specified HTTP request asynchronously, targeting the provided HTTP host. The application may optionally provide a data object, to be returned in the HTTP response event, for instance an object which uniquely identifies the request sent.
endActivity()
method:
Ends the activity and its related Activity Context.
There is a single event fired by HTTP Client Resource Adaptor, which represents a response to a request, received in a specific HttpClientActivity
instance.
Table 2.1. Events fired on the HttpClientActivity
Name | Vendor | Version | Event Class | Description |
---|---|---|---|---|
net.java. client.slee. resource.http. event. ResponseEvent | net.java. client.slee | 4.0 | net.java. client.slee. resource.http. event. ResponseEvent | A response event to an asynchronous HTTP request. |
Spaces where introduced in Name
, Vendor
and Event Class
column values, to correctly render the table. Please remove them when using copy/paste.
The response event class provides the result of sending an HTTP request, which can be response or exception, due to error.
The Resource Adaptor's Activity Context Interface Factory is of type net.java.client.slee.resource.http.HttpClientActivityContextInterfaceFactory
, it allows the SBB to retrieve the ActivityContextInterface
related with an existing Resource Adaptor activity object. The interface is defined as follows:
package net.java.client.slee.resource.http;
import javax.slee.ActivityContextInterface;
import javax.slee.FactoryException;
import javax.slee.UnrecognizedActivityException;
public interface HttpClientActivityContextInterfaceFactory {
public ActivityContextInterface getActivityContextInterface(
HttpClientActivity acivity) throws NullPointerException,
UnrecognizedActivityException, FactoryException;
}
The HTTP Client Resource Adaptor interface, of type net.java.client.slee.resource.http.HttpClientResourceAdaptorSbbInterface
, which an SBB uses to create new HttpClientActivity
instances or get access the managed HTTP Client, to send synchronous requests or change its parameters, its interface is defined as follows:
package net.java.client.slee.resource.http;
import java.io.IOException;
import javax.slee.resource.StartActivityException;
import net.java.client.slee.resource.http.event.Response;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.params.HttpClientParams;
public interface HttpClientResourceAdaptorSbbInterface {
public HttpClient getHttpClient();
public HttpClientActivity createHttpClientActivity(
boolean endOnReceivingResponse, HttpContext context)
throws StartActivityException;
}
createHttpClientActivity(boolean, HttpContext)
method:
Creates a new
HttpClientActivity
instance. If the first parameter is true then the activity will end once
the HTTP Request response is received, if false then the SBB needs
to end the activity through its endActivity() method. An optional HttpContext may also be specified, if not, a basic context will be set by the RA.
getHttpClient()
method:
Retrieves the client managed by the RA, allowing execution of synchronous requests and access the client parameters. Note that the returned client throws a SecurityException if the application tries to access its ClientConnectionManager.
The managed HTTP Client, which may be obtained from HTTP Client Resource Adaptor SBB Interface, throws a SecurityException if the application tries to access its ClientConnectionManager.
The following code examples shows how to use the Resource Adaptor Type for common functionalities
The following code examples the usage of the RA's SBB Interface to send synchronous HTTP requests:
HttpGet request = new HttpGet(syndFeed.getLink());
try {
Response response = raSbbInterface.getHttpClient.execute(request);
} catch (Throwable e) {
tracer.severe("Error while sending request",e);
}
The following code examples the creation and usage of the HttpClientActivity to send an async HTTP GET requests the optimal way to use the RA, since it doesn't block the SLEE container event routing threads:
HttpGet request = new HttpGet(syndFeed.getLink());
try {
HttpClientActivity clientActivity = raSbbInterface
.createHttpClientActivity(true,null);
ActivityContextInterface clientAci = httpClientAci
.getActivityContextInterface(clientActivity);
clientAci.attach(sbbContext.getSbbLocalObject());
clientActivity.execute(request,syndFeed.getLink());
} catch (Throwable e) {
tracer.severe("Error while creating HttpClientActivity",e);
}
And the following code examples the handling of the response event:
public void onResponseEvent(ResponseEvent event,
ActivityContextInterface aci) {
HttpResponse response = event.getHttpResponse();
tracer.info("********** onResponseEvent **************");
tracer.info("URI = " + event.getRequestApplicationData());
tracer.info("Status Code = " +
response.getStatusLine().getStatusCode());
try {
tracer.info("Response Body = "
+ EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
tracer.severe("Failed reading response body", e);
}
tracer.info("*****************************************");
}