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>

AtomicLong的更多相关文章

  1. Java多线程系列--“JUC原子类”02之 AtomicLong原子类

    概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...

  2. 多线程爬坑之路-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析

    Atomic原子类:为基本类型的封装类Boolean,Integer,Long,对象引用等提供原子操作. 一.Atomic包下的所有类如下表: 类摘要 AtomicBoolean 可以用原子方式更新的 ...

  3. [JDK8]性能优化之使用LongAdder替换AtomicLong

    如果让你实现一个计数器,有点经验的同学可以很快的想到使用AtomicInteger或者AtomicLong进行简单的封装. 因为计数器操作涉及到内存的可见性和线程之间的竞争,而Atomic***的实现 ...

  4. JDK1.8 LongAdder 空间换时间: 比AtomicLong还高效的无锁实现

    我们知道,AtomicLong的实现方式是内部有个value 变量,当多线程并发自增,自减时,均通过CAS 指令从机器指令级别操作保证并发的原子性. // setup to use Unsafe.co ...

  5. java多线程之AtomicLong与LongAdder

    AtomicLong简要介绍 AtomicLong是作用是对长整形进行原子操作,显而易见,在java1.8中新加入了一个新的原子类LongAdder,该类也可以保证Long类型操作的原子性,相对于At ...

  6. 使用AtomicLong,经典银行账户问题

    1.新建Account类,使用AtomicLong定义账户余额,增加和减少金额方法使用getAndAdd方法. package com.xkzhangsan.atomicpack.bank; impo ...

  7. AtomicLong和LongAdder的区别

    AtomicLong的原理是依靠底层的cas来保障原子性的更新数据,在要添加或者减少的时候,会使用死循环不断地cas到特定的值,从而达到更新数据的目的. LongAdder在AtomicLong的基础 ...

  8. 【Java多线程】AtomicLong和LongAdder

    AtomicLong简要介绍 AtomicLong是作用是对长整形进行原子操作,显而易见,在java1.8中新加入了一个新的原子类LongAdder,该类也可以保证Long类型操作的原子性,相对于At ...

  9. AtomicLong.lazySet 是如何工作的?

    原文:http://www.quora.com/Java-programming-language/How-does-AtomicLong-lazySet-work Jackson Davis说:为一 ...

随机推荐

  1. c#中怎么删除一个非空目录

    System.IO.Directory.Delete(@"C:\abc\",true)

  2. [转载]MongoDB的真正性能

    最近开始研究MySQL和MongoDB,发现这方面资料不多.尤其是真正的说到点子上的文章,太少了. 有一些对比测试的文章基本上都是瞎测,测试方法都测到了马腿上,得出的结论基本上都是NoSQL毫无价值 ...

  3. tomcat 端口被占用

    找到占用8080端口的是PID为 2392的进程,于是 ctrl +shift+esc ,然后将这个进程结束掉.

  4. [TSP+floyd]POJ3311 Hie with the Pie

    题意: 给i到j花费的地图 1到n编号   一个人要从1遍历n个城市后回到1 求最小的花费(可以重复走) 分析 http://www.cnblogs.com/Empress/p/4039240.htm ...

  5. 乐1/MACBOOK/ N1 Type-C接口新体验

    经过在华强北电子市场排队一个小时,笔者顺利买到了期待已久的乐1,结合之前的NOKIA平板电脑N1,苹果全新MACBOOK,终于集齐了手机.平板.笔记本电脑三种TYPE-C接口设备(能兑换极品装备吗?^ ...

  6. VC++下封装ADO类以及使用方法

    操作系统:windows 7软件环境:visual studio 2008 .Microsoft SQL 2005本次目的:介绍一个已经封装的ADO类,简单说明怎么导入使用 首先声明一下,这个封装的A ...

  7. bzoj3144

    最小割解决最优值最突出的特点就是要将对象划分到两个集合中这题很明显,裸的最小割先把点连成一根根柱子,就是p(x,y,k)-->p(x,y,k+1)流量是P(x,y,k+1)的不和谐值然后s与p( ...

  8. bzoj1443

    首先要思考的问题肯定是,什么点可以是ans, 不难想到对图黑白染色,假如一个棋子不管怎么走,都只能走到和它同色的点上时,这就是一个ans 再考虑,每次棋子的移动都是黑白相间的 再考虑,黑白染色是可以构 ...

  9. Sharepoint 2010 用VS定制Master,并且每个Web应用同一个Master

    转:http://***/html/blogs/20130407/1381.htm 最近做了一个项目管理系统,要求用Sharepoint,有个特别的功能就是通过创建出来的子站点要求应用同一个Maste ...

  10. 如何使用eclipse进行嵌入式Linux的开发

    如何使用eclipse进行嵌入式Linux的开发 作者:曾宏安,华清远见嵌入式学院高级讲师. 如何使用eclipse进行嵌入式Linux的开发 习惯了在windows环境下开发的程序员在转到Linux ...