package com.ws.service.syn;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("counter")
public class Counter {
public int counter = 0;
@GET
@Path("/count")
public String getCount(){
counter++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return String.valueOf(counter);
}
public int get(){
return counter;
}
public void set(int num){
this.counter = num;
}
@GET
@Path("/hello")
public String hello(){
return "xjiewenbo";
}
@GET
@Path("/{num}")
@Produces("text/plain")
public String getCounter(@PathParam("num")String num){
set(Integer.parseInt(num));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return String.valueOf(get());
}
}
public static void main(String[] args) throws IOException {
URI ServerURI=UriBuilder.fromUri("http://localhost/").port(9999).build();
startServer(ServerURI);
System.out.println("服务已启动,请访问:"+ServerURI);
}
protected static SelectorThread startServer(URI serverURI) throws IOException {
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages","com.ws.service.syn");
System.out.println("Grizzly 启动中...");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(serverURI, initParams);
return threadSelector;
}
}