package demo.helloprovider;
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import demo.hellocommon.*;

import org.apache.dubbo.config.annotation.Argument;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

//@DubboService(timeout=3000,retries=3,loadbalance="roundrobin")
@DubboService(
  timeout=10000,
  methods={@Method(name="addListener",arguments = {@Argument(index=1,callback = true)})}
)
public class HelloServiceImpl implements HelloService {
    private final Map<String, CallbackListener> listeners = new ConcurrentHashMap<String, CallbackListener>();

    public HelloServiceImpl() {
        Thread t = new Thread(new Runnable() {
            public void run() {
                while(true) {
                    try {
                        for(Map.Entry<String, CallbackListener> entry : listeners.entrySet()){
                            try {
                                entry.getValue().report("当前时间："+new Date());
                            } catch (Throwable t) {
                                listeners.remove(entry.getKey());
                            }
                        }
                        Thread.sleep(5000); // 定时触发
                    } catch (Throwable t) { // 防御容错
                        t.printStackTrace();
                    }
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }

    @Override
    public void addListener(String key, CallbackListener listener) {
        listeners.put(key, listener);
    }

    @Override
    public String sayHello(String username,String key){
        CallbackListener listener=listeners.get(key);
        listener.report(username+"打过招呼。");
        return "Hello," + username;
     }



/*
    @Override
    public String sayHello(String username){
      try{
        //Thread.sleep(10000);
      }catch(Exception e){e.printStackTrace();}
      return "Hello,"+username;
    }
*/


    @Value("${server.port}")
    private String servicePort;

    @Value("${spring.application.name}")
    private String serviceName;

    @Override
    public String sayHello(String username){
        return "Hello," + username
               +"<br>Service Name:" + serviceName+"<br>Service Port:"+servicePort;
    }


/*
    @Override
    @SentinelResource(value="say",blockHandler = "handleBlock")
    public String sayHello(String username){
        return "Hello," + username
                +"<br>Service Name:" + serviceName+"<br>Service Port:"+servicePort;
    }

    public String handleBlock(String username,
                              BlockException ex) {
        ex.printStackTrace();
        return username+",request is blocked.";
    }
*/
    @Override
    public Result testUser(User user) {
        System.out.println("id:"+user.getId()
                +",name:"+user.getName().getFirstname()
                +" "+user.getName().getLastname());

        return new Result(100,"OK");
    }

    public String doTask(String longtask){
       try{
          Thread.sleep(5000);
       } catch (Exception ex){
          ex.printStackTrace();
       }
        return longtask+"已经完成";
    }

}


/*********************************************************
 * 作者：孙卫琴                                   
 * 来源：《Spring Cloud Alibaba微服务开发零基础入门到实战》源代码  
 * 技术支持网址：www.javathinker.net/alibaba                    
 *********************************************************/
