zoukankan      html  css  js  c++  java
  • 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> 
  • 相关阅读:
    通过AI识图判断图片是否为小票
    orcl 定时器
    防止表单重复提交常规方法
    代码规范案例(ssh分页)
    jquery的ajax提交时“加载中”提示的处理方法
    利用JS弹出层实现简单的动态提示“正在加载中,请稍等...”
    jquery 排序table的列
    Java多线程与网络编程综合使用
    大型Oracle数据库设计方案(精华)
    20155307 2016-2017-2 《Java程序设计》第10周学习总结
  • 原文地址:https://www.cnblogs.com/exmyth/p/5199653.html
Copyright © 2011-2022 走看看