Friday, October 4, 2013

REST Webservice on NETTY RESTEasy



I had to implement some simple REST service
Requirments were:
1. low latancy
2. highly scalable
3. robust
4. high throughput
5. simple
6. JSON based parameters passing
Ive started with Tomcat with servlet on top of it and got bad throughput.
Ive tried Jetty but still - throughpit was terreble.
Then i decided to use Netty with some REST back on top of it.
Will do benchmarking and update you soon....

Resteasy
http://www.jboss.org/resteasy

Service :


@Path("/message")
public class RestService {

@Path("/test")
@POST
@Consumes("application/json")
@Produces("application/json")
public Response addOrder(Container a)
{
return Response.status(200).entity("DONE").build();
}

}

@XmlRootElement
public class Container
{

private ArrayList<Parameter> parameters = new ArrayList<>();
public void AddParameter(Parameter p)
{
this.parameters.add(p);
}
@XmlElement
public ArrayList<Parameter> getParameters() {
return parameters;
}
public void setParameters(ArrayList<Parameter> parameters) {
this.parameters = parameters;
}

}

@XmlRootElement
public class Parameter {

private String name;
private int age;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

public class Server 
{
public static void main(String[] args) {
ResteasyDeployment deployment = new ResteasyDeployment();
  Map<String, String> mediaTypeMappings = new HashMap<String, String>();
  mediaTypeMappings.put("xml", "application/xml");
  mediaTypeMappings.put("json", "application/json");
  deployment.setMediaTypeMappings(mediaTypeMappings);
  NettyJaxrsServer netty = new NettyJaxrsServer();
   netty.setDeployment(deployment);
   netty.setPort(TestPortProvider.getPort());
   netty.setRootResourcePath("");
   netty.setSecurityDomain(null);
   netty.start();
  deployment.getRegistry().addPerRequestResource(RestService.class);

}

}

Client :

public class ClientMain {
public static void main(String[] args) {
Client client = ClientBuilder.newBuilder().build();
       WebTarget target = client.target("http://localhost:8081/message/test");
       Container c = new Container();
       Parameter param = new Parameter();
       param.setAge(11);
       param.setName("RAMI");
       c.AddParameter(param);
       Response response = target.request().post(Entity.entity(c, "application/json"));
       String value = response.readEntity(String.class);
       System.out.println(value);
       response.close();  
}
}

No comments:

Post a Comment