AtomicLong
Spring
package com.uniubi.management.controller; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.uniubi.management.model.Greeting; @RestController public class GreetingController { private static final String template = "Hello,%s"; private final AtomicLong counter = new AtomicLong(); //http://localhost:8080/management/greeting.do @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { return new Greeting(counter.getAndIncrement(), String.format(template, name)); } }
package com.uniubi.management.controller; import java.io.IOException; import java.io.PrintWriter; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.AsyncRestTemplate; import com.alibaba.fastjson.JSON; import com.uniubi.management.model.Greeting; import com.uniubi.management.util.HttpClientUtil; @RestController @RequestMapping("/api") public class ApiController { // private static final String template = "Hello,%s"; // private final AtomicLong counter = new AtomicLong(); private AsyncRestTemplate template; @PostConstruct public void init() { System.out.println("-----------------ApiController init"); template = new AsyncRestTemplate(); template.setAsyncRequestFactory(new HttpComponentsAsyncClientHttpRequestFactory()); } @PreDestroy public void destory() { System.out.println("-----------------ApiController destory"); } //http://localhost:8080/management/greeting.do @RequestMapping("/test") public Greeting test(@RequestParam(value = "username", defaultValue = "World") String username) throws IOException { String result = HttpClientUtil.doGet("http://localhost:8080/management/greeting.do"); return JSON.parseObject(result, Greeting.class); } @RequestMapping("/test2") public void test2(@RequestParam(value = "username", defaultValue = "World") String username, HttpServletRequest request, HttpServletResponse response) throws IOException { // 调用完后立即返回(没有阻塞) ListenableFuture<ResponseEntity<Greeting>> future = template.getForEntity("http://localhost:8080/management/greeting.do", Greeting.class); // 设置异步回调 future.addCallback(new ListenableFutureCallback<ResponseEntity<Greeting>>() { @Override public void onSuccess(ResponseEntity<Greeting> result) { System.out.println("----"+JSON.toJSONString(result.getBody())); try { PrintWriter writer = response.getWriter(); writer.write(JSON.toJSONString(result.getBody())); writer.flush(); } catch (IOException e) { e.printStackTrace(); } /* finally{ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } */ } @Override public void onFailure(Throwable t) { try { PrintWriter writer = response.getWriter(); writer.write("{a:1}"); writer.flush(); } catch (IOException e) { e.printStackTrace(); } /* finally { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } */ } }); } }
package com.uniubi.management.controller; import org.springframework.http.ResponseEntity; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback; import org.springframework.web.client.AsyncRestTemplate; import com.uniubi.management.model.Greeting; public class GreetingControllerTest { public static void main(String[] args) { AsyncRestTemplate template = new AsyncRestTemplate(); // 调用完后立即返回(没有阻塞) ListenableFuture<ResponseEntity<Greeting>> future = template.getForEntity("http://localhost:8080/management/greeting.do", Greeting.class); // 设置异步回调 future.addCallback(new ListenableFutureCallback<ResponseEntity<Greeting>>() { @Override public void onSuccess(ResponseEntity<Greeting> result) { System.out.println("======client get result : " + result.getBody()); } @Override public void onFailure(Throwable t) { System.out.println("======client failure : " + t); } }); System.out.println("==no wait"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" /> <bean id="getInfoServiceImpl" class="com.uniubi.management.ws.impl.GetInfoServiceImpl"></bean> <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint> </beans>