/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.core.test;

import com.crunii.micro.common.spring.rest.RestBuilder;
import com.crunii.micro.common.utils.StopUtil;
import com.crunii.micro.core.dto.ApiUserDemo;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 田平 create 2019年8月28日下午4:49:59
 */
public class Test2 {

	public static void main(String[] args) throws Exception {
//		List<String> orderIds = Files.lines(Paths.get("D:\\qq-files\\qq-messages\\48690326\\FileRecv\\n00_oa_reply_20190828021039.dat"), Charset.forName("GBK")).collect(Collectors.toList());
//		orderIds.stream().forEach(s -> {
//			System.out.println(s);
//		});

		ExecutorService executor = Executors.newCachedThreadPool();
		ListeningExecutorService decorator = MoreExecutors.listeningDecorator(executor);

		Futures.addCallback(decorator.submit(() -> {
			String url = "http://localhost:9995/TestController/testNoBody";
			RestTemplate rt = RestBuilder.buildRestTemplate();
			ApiUserDemo demo = new ApiUserDemo();
			demo.setUsername("TTTTTTTTT");

			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_JSON);
			headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
			HttpEntity<ApiUserDemo> entity = new HttpEntity<ApiUserDemo>(demo, headers);

			rt.exchange(url, HttpMethod.POST, entity, Void.class);
			//System.out.println(exchange);
			return true;
		}), new FutureCallback<Boolean>() {
			@Override
			public void onSuccess(@Nullable Boolean result) {
				System.out.println(result);
			}

			@Override
			public void onFailure(Throwable t) {
				System.out.println("ERROR");
			}
		}, MoreExecutors.directExecutor());

		StopUtil.stopES(executor);
	}
}
