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> 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值