Example #1
0
 private void referFields(Object bean, Field[] fields) {
   for (Field field : fields) {
     try {
       if (!field.isAccessible()) {
         field.setAccessible(true);
       }
       Reference reference = field.getAnnotation(Reference.class);
       if (reference != null) {
         Object value = refer(reference, field.getType());
         if (value != null) {
           field.set(bean, value);
         }
       }
     } catch (Throwable e) {
       logger.error(
           "Failed to init remote service reference at field "
               + field.getName()
               + " in class "
               + bean.getClass().getName()
               + ", cause: "
               + e.getMessage(),
           e);
     }
   }
 }
  @Override
  public void connect() throws NetworkException {
    connectLock.lock();
    try {
      if (isAvaliable()) {
        logger.info("[connect] is connected to remote " + remoteAddress + ".");
        return;
      }

      ChannelFuture future = bootstrap.connect(remoteAddress);

      try {
        if (future.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS)) {

          if (future.isSuccess()) {
            disConnect();
            this.channel = future.getChannel();
            localAddress = (InetSocketAddress) this.channel.getLocalAddress();
          } else {
            logger.info("[connect] connected to remote " + remoteAddress + " failed.");
            throw new NetworkException("connected to remote " + remoteAddress + " failed.");
          }

        } else {
          logger.info("[connect] timeout connecting to remote " + remoteAddress + ".");
          throw new NetworkException("timeout connecting to remote " + remoteAddress + ".");
        }

      } catch (Throwable e) {
        logger.info("[connect] error connecting to remote " + remoteAddress + ".", e);

        throw new NetworkException("error connecting to remote " + remoteAddress + ".", e);
      } finally {
        if (!isConnected()) {
          future.cancel();
        }
      }
    } finally {
      connectLock.unlock();
    }
  }
 @Override
 public void disConnect() {
   connectLock.lock();
   try {
     if (this.channel != null) {
       this.channel.close();
     }
   } catch (Throwable e) {
     logger.error("[disConnect] error disConnecting channel. ", e);
   } finally {
     connectLock.unlock();
   }
 }
  public static void main(String[] args) {
    Quality quality = new Quality(105, 1);
    System.out.println(quality.getQualityValue());
    final ConcurrentHashMap<String, ConcurrentHashMap<String, Quality>> addrReqUrlQualities =
        new ConcurrentHashMap<String, ConcurrentHashMap<String, Quality>>();
    new Thread() {
      @Override
      public void run() {
        RequestQualityManager.INSTANCE.setAddrReqUrlQualities(addrReqUrlQualities);
      }
    }.start();

    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      logger.warn("[main] thread interupt", e);
    }
    System.out.println(RequestQualityManager.INSTANCE.getAddrReqUrlQualities());
  }
Example #5
0
  public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {
    if (!isMatchPackage(bean)) {
      return bean;
    }
    Method[] methods = bean.getClass().getMethods();
    for (Method method : methods) {
      String name = method.getName();
      if (name.length() > 3
          && name.startsWith("set")
          && method.getParameterTypes().length == 1
          && Modifier.isPublic(method.getModifiers())
          && !Modifier.isStatic(method.getModifiers())) {
        try {
          Reference reference = method.getAnnotation(Reference.class);
          if (reference != null) {
            Object value = refer(reference, method.getParameterTypes()[0]);
            if (value != null) {
              method.invoke(bean, new Object[] {});
            }
          }
        } catch (Throwable e) {
          logger.error(
              "Failed to init remote service reference at method "
                  + name
                  + " in class "
                  + bean.getClass().getName()
                  + ", cause: "
                  + e.getMessage(),
              e);
        }
      }
    }
    Class<?> superClass = bean.getClass().getSuperclass();
    while (superClass != null && isMatchPackage(superClass)) {
      referFields(bean, superClass.getDeclaredFields());
      superClass = superClass.getSuperclass();
    }
    referFields(bean, bean.getClass().getDeclaredFields());

    return bean;
  }