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. 用 JMH 检测 Lambdas 序列化性能

    本文将介绍如何进行 Java Lambdas 序列化性能检测.Lambdas 的重要性以及 Lambdas 在分布式系统中的应用. Lambdas 表达式是 Java 8 中万众期待的新特性,其若干用 ...

  2. ****Objective-C 中的方法的调用

    oc语言中采用特定的语言调用类或者实例(对象)的方法称为发送消息或者方法调用. oc中方法的调用有两种: 第一种: [类名或对象名 方法名]; [ClassOrInstance method]; [C ...

  3. UVA 10801 Lift Hopping

    算是一道需要动脑筋的最短路问题了,关键在于建图部分,对于n个电梯中每一个都要经过cnt个楼层,a[0],a[1],a[2],a[3],a[4],......a[cnt-1],那么对于任意两个楼层a[j ...

  4. HTTP Header 入门详解

    什么是HTTP Headers HTTP是"Hypertext Transfer Protocol"的所写,整个www都在使用这种协定,几乎你在流览器里看到的大部分内容都是通过ht ...

  5. Earth Mover's Distance (EMD)

    原文: http://d.hatena.ne.jp/aidiary/20120804/1344058475作者: sylvan5翻译: Myautsai和他的朋友们(Google Translate. ...

  6. Linux内核spin_lock与spin_lock_irq分析

    http://blog.csdn.net/zhanglei4214/article/details/6837697

  7. [unity菜鸟] 修改发布成web后的logo

    1. 原始效果  (tip:在4.x的书中有介绍) 2. 打开.html文件原始代码如下 <script type='text/javascript' src='jquery.min.js'&g ...

  8. asp.net 框架初接触

    1. 在web层的Default.aspx里只有最基本的UI代码 2. 在web层的Default.aspx.cs里第一行创建一个用户业务对象UserBO (注意添加引用,下同) protected ...

  9. Android Button悬浮在SurfaceView上

    实现Button悬浮于与SurfaceView之上实现 注意:你实现的SurfaceView和android中的Button,EditView是同级的,不能把一个包含在另一个里面 1.创建自己的Sur ...

  10. int指令理解

    以下是王爽老师的<汇编语言>中第十五章中的一段程序代码,其功能是增加9号中断的功能,当按下Esc键时屏幕中显示的字母改变颜色 assume cs:codesg,ss:stack,ds:da ...