Java Code Examples for com.alibaba.dubbo.rpc.Invoker#destroy()

The following examples show how to use com.alibaba.dubbo.rpc.Invoker#destroy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumBak.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericEnum() throws InterruptedException {
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE
    );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);

    URL consumerurl = serviceurl;

    Invoker<GenericService> reference = protocol.refer(GenericService.class, consumerurl);

    GenericService demoProxy = (GenericService) proxy.getProxy(reference);
    Object obj = demoProxy.$invoke("enumlength", new String[]{Type[].class.getName()}, new Object[]{new Type[]{Type.High, Type.High}});
    System.out.println("obj---------->" + obj);

    invoker.destroy();
    reference.destroy();
}
 
Example 2
Source File: EnumBak.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
    public void testNormal(){
        int port = NetUtils.getAvailablePort();
        URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?proxy=jdk" 
                + "&interface=" + DemoService.class.getName()
        		+ "&timeout=" + Integer.MAX_VALUE
                );
        DemoService demo = new DemoServiceImpl();
        Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
        protocol.export(invoker);
        
        URL consumerurl = serviceurl;
        Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
        DemoService demoProxy = (DemoService)proxy.getProxy(reference);
//        System.out.println(demoProxy.getThreadName());
        Assert.assertEquals((byte)-128, demoProxy.getbyte((byte)-128));
        
//        invoker.destroy();
        reference.destroy();
    }
 
Example 3
Source File: HessianProtocolTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundException {
    HessianServiceImpl server = new HessianServiceImpl();
    Assert.assertFalse(server.isCalled());
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&generic=nativejava");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxyFactory.getProxy(invoker);

    Serialization serialization = new NativeJavaSerialization();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject("haha");
    objectOutput.flushBuffer();

    Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{byteArrayOutputStream.toByteArray()});
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream((byte[]) result);
    ObjectInput objectInput = serialization.deserialize(url, byteArrayInputStream);
    Assert.assertTrue(server.isCalled());
    Assert.assertEquals("Hello, haha", objectInput.readObject());
    invoker.destroy();
    exporter.unexport();
}
 
Example 4
Source File: HessianProtocolTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericInvokeWithRpcContext() {
    RpcContext.getContext().setAttachment("myContext", "123");

    HessianServiceImpl server = new HessianServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxyFactory.getProxy(invoker, true);
    String result = (String) client.$invoke("context", new String[]{"java.lang.String"}, new Object[]{"haha"});
    Assert.assertEquals("Hello, haha context, 123", result);
    invoker.destroy();
    exporter.unexport();
}
 
Example 5
Source File: HessianProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomException() {
    HessianServiceImpl server = new HessianServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    try {
        client.customException();
        fail();
    } catch (MyException expected) {
    }
    invoker.destroy();
    exporter.unexport();
}
 
Example 6
Source File: HessianProtocolTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClient() {
    HessianServiceImpl server = new HessianServiceImpl();
    Assert.assertFalse(server.isCalled());
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&client=httpclient&hessian.overload.method=true");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    String result = client.sayHello("haha");
    Assert.assertTrue(server.isCalled());
    Assert.assertEquals("Hello, haha", result);
    invoker.destroy();
    exporter.unexport();
}
 
Example 7
Source File: HessianProtocolTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClient() {
    HessianServiceImpl server = new HessianServiceImpl();
    Assert.assertFalse(server.isCalled());
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&client=httpclient");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    String result = client.sayHello("haha");
    Assert.assertTrue(server.isCalled());
    Assert.assertEquals("Hello, haha", result);
    invoker.destroy();
    exporter.unexport();
}
 
Example 8
Source File: HessianProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClient() {
    HessianServiceImpl server = new HessianServiceImpl();
    Assert.assertFalse(server.isCalled());
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&client=httpclient");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    String result = client.sayHello("haha");
    Assert.assertTrue(server.isCalled());
    Assert.assertEquals("Hello, haha", result);
    invoker.destroy();
    exporter.unexport();
}
 
Example 9
Source File: EnumBak.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
    public void testNormal(){
        int port = NetUtils.getAvailablePort();
        URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?proxy=jdk" 
                + "&interface=" + DemoService.class.getName()
        		+ "&timeout=" + Integer.MAX_VALUE
                );
        DemoService demo = new DemoServiceImpl();
        Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
        protocol.export(invoker);
        
        URL consumerurl = serviceurl;
        Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
        DemoService demoProxy = (DemoService)proxy.getProxy(reference);
//        System.out.println(demoProxy.getThreadName());
        Assert.assertEquals((byte)-128, demoProxy.getbyte((byte)-128));
        
//        invoker.destroy();
        reference.destroy();
    }
 
Example 10
Source File: HessianProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeOut() {
    HessianServiceImpl server = new HessianServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&timeout=10");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    try {
        client.timeOut(6000);
        fail();
    } catch (RpcException expected) {
        Assert.assertEquals(true, expected.isTimeout());
    }finally{
        invoker.destroy();
        exporter.unexport();
    }
    
}
 
Example 11
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭所有Invoker
 */
private void destroyAllInvokers() {
    Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
    if(localUrlInvokerMap != null) {
        for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) {
            try {
                invoker.destroy();
            } catch (Throwable t) {
                logger.warn("Failed to destroy service " + serviceKey + " to provider " + invoker.getUrl(), t);
            }
        }
        localUrlInvokerMap.clear();
    }
    methodInvokerMap = null;
}
 
Example 12
Source File: ProtocolFilterWrapper.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static <T> Invoker<T> buildInvokerChain(final Invoker<T> invoker, String key, String group) {
    Invoker<T> last = invoker;
    List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
    if (filters.size() > 0) {
        for (int i = filters.size() - 1; i >= 0; i --) {
            final Filter filter = filters.get(i);
            final Invoker<T> next = last;
            last = new Invoker<T>() {

                public Class<T> getInterface() {
                    return invoker.getInterface();
                }

                public URL getUrl() {
                    return invoker.getUrl();
                }

                public boolean isAvailable() {
                    return invoker.isAvailable();
                }

                public Result invoke(Invocation invocation) throws RpcException {
                    return filter.invoke(next, invocation);
                }

                public void destroy() {
                    invoker.destroy();
                }

                @Override
                public String toString() {
                    return invoker.toString();
                }
            };
        }
    }
    return last;
}
 
Example 13
Source File: EnumBak.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public void testEnumCompat(){
    int port = 20880;
    URL consumerurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
    );
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService)proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);
    reference.destroy();
}
 
Example 14
Source File: StaticDirectory.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void destroy() {
    if(isDestroyed()) {
        return;
    }
    super.destroy();
    for (Invoker<T> invoker : invokers) {
        invoker.destroy();
    }
    invokers.clear();
}
 
Example 15
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 检查缓存中的invoker是否需要被destroy
 * 如果url中指定refer.autodestroy=false,则只增加不减少,可能会有refer泄漏,
 * 
 * @param invokers
 */
private void destroyUnusedInvokers(Map<String, Invoker<T>> oldUrlInvokerMap, Map<String, Invoker<T>> newUrlInvokerMap) {
    if (newUrlInvokerMap == null || newUrlInvokerMap.size() == 0) {
        destroyAllInvokers();
        return;
    }
    // check deleted invoker
    List<String> deleted = null;
    if (oldUrlInvokerMap != null) {
        Collection<Invoker<T>> newInvokers = newUrlInvokerMap.values();
        for (Map.Entry<String, Invoker<T>> entry : oldUrlInvokerMap.entrySet()){
            if (! newInvokers.contains(entry.getValue())) {
                if (deleted == null) {
                    deleted = new ArrayList<String>();
                }
                deleted.add(entry.getKey());
            }
        }
    }
    
    if (deleted != null) {
        for (String url : deleted){
            if (url != null ) {
                Invoker<T> invoker = oldUrlInvokerMap.remove(url);
                if (invoker != null) {
                    try {
                        invoker.destroy();
                        if(logger.isDebugEnabled()){
                            logger.debug("destory invoker["+invoker.getUrl()+"] success. ");
                        }
                    } catch (Exception e) {
                        logger.warn("destory invoker["+invoker.getUrl()+"] faild. " + e.getMessage(), e);
                    }
                }
            }
        }
    }
}
 
Example 16
Source File: FailoverClusterInvokerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
   * 测试在调用重试过程中,directory列表变更,invoke重试时重新进行list选择 
   */
  @Test
  public void testInvokerDestoryAndReList(){
  	final URL url = URL.valueOf("test://localhost/"+ Demo.class.getName() + "?loadbalance=roundrobin&retries="+retries);
  	RpcException exception = new RpcException(RpcException.TIMEOUT_EXCEPTION);
  	MockInvoker<Demo> invoker1 = new MockInvoker<Demo>(Demo.class, url);
  	invoker1.setException(exception);
  	
  	MockInvoker<Demo> invoker2 = new MockInvoker<Demo>(Demo.class, url);
  	invoker2.setException(exception);
  	
      final List<Invoker<Demo>> invokers = new ArrayList<Invoker<Demo>>();
      invokers.add(invoker1);
      invokers.add(invoker2);
      
      Callable<Object> callable = new Callable<Object>() {
	public Object call() throws Exception {
		//模拟invoker全部被destroy掉
		for (Invoker<Demo> invoker:invokers){
			invoker.destroy();
		}
		invokers.clear();
		MockInvoker<Demo> invoker3  = new MockInvoker<Demo>(Demo.class, url);
		invokers.add(invoker3);
		return null;
	}
};
invoker1.setCallable(callable);
      invoker2.setCallable(callable);
      
      RpcInvocation inv = new RpcInvocation();
      inv.setMethodName("test");
      
      Directory<Demo> dic = new MockDirectory<Demo>(url, invokers);
      
      FailoverClusterInvoker<Demo> clusterinvoker = new FailoverClusterInvoker<Demo>(dic);
      clusterinvoker.invoke(inv);
  }
 
Example 17
Source File: RegistryDirectory.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭所有Invoker
 */
private void destroyAllInvokers() {
    Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
    if(localUrlInvokerMap != null) {
        for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) {
            try {
                invoker.destroy();
            } catch (Throwable t) {
                logger.warn("Failed to destroy service " + serviceKey + " to provider " + invoker.getUrl(), t);
            }
        }
        localUrlInvokerMap.clear();
    }
    methodInvokerMap = null;
}
 
Example 18
Source File: EnumBak.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void testEnumCompat(){
    int port = 20880;
    URL consumerurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
    );
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService)proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);
    reference.destroy();
}
 
Example 19
Source File: FailoverClusterInvokerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
   * 测试在调用重试过程中,directory列表变更,invoke重试时重新进行list选择 
   */
  @Test
  public void testInvokerDestoryAndReList(){
  	final URL url = URL.valueOf("test://localhost/"+ Demo.class.getName() + "?loadbalance=roundrobin&retries="+retries);
  	RpcException exception = new RpcException(RpcException.TIMEOUT_EXCEPTION);
  	MockInvoker<Demo> invoker1 = new MockInvoker<Demo>(Demo.class, url);
  	invoker1.setException(exception);
  	
  	MockInvoker<Demo> invoker2 = new MockInvoker<Demo>(Demo.class, url);
  	invoker2.setException(exception);
  	
      final List<Invoker<Demo>> invokers = new ArrayList<Invoker<Demo>>();
      invokers.add(invoker1);
      invokers.add(invoker2);
      
      Callable<Object> callable = new Callable<Object>() {
	public Object call() throws Exception {
		//模拟invoker全部被destroy掉
		for (Invoker<Demo> invoker:invokers){
			invoker.destroy();
		}
		invokers.clear();
		MockInvoker<Demo> invoker3  = new MockInvoker<Demo>(Demo.class, url);
		invokers.add(invoker3);
		return null;
	}
};
invoker1.setCallable(callable);
      invoker2.setCallable(callable);
      
      RpcInvocation inv = new RpcInvocation();
      inv.setMethodName("test");
      
      Directory<Demo> dic = new MockDirectory<Demo>(url, invokers);
      
      FailoverClusterInvoker<Demo> clusterinvoker = new FailoverClusterInvoker<Demo>(dic);
      clusterinvoker.invoke(inv);
  }
 
Example 20
Source File: JmsProtocolTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
  public void testHessianProtocol() throws URISyntaxException, Exception {

ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();

//Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
//Invoker<DemoService> invokerp = protocol.refer(DemoService.class, URL.valueOf("jms://" + DemoService.class.getName() + "?version=1.0.0"));
      
XBeanBrokerFactory factory = new XBeanBrokerFactory();
BrokerService broker = factory.createBroker(new URI(JmsProtocolTest.class.getPackage().getName().replace('.', '/') +"/activemq.xml"));
broker.start();

ActiveMQConnectionFactory queueConnectionFactory = new ActiveMQConnectionFactory( "tcp://localhost:61617" );
Queue queue = new ActiveMQQueue("DUBBO");

JmsProtocol jmsProtocol = new JmsProtocol();
jmsProtocol.setQueue(queue);
jmsProtocol.setQueueConnectionFactory(queueConnectionFactory);

      DemoServiceImpl server = new DemoServiceImpl();
      
      URL url = URL.valueOf("jms://" + DemoService.class.getName() + "?version=1.0.0");
      Exporter<DemoService> exporter = jmsProtocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
      Invoker<DemoService> invoker = jmsProtocol.refer(DemoService.class, url);
      DemoService client = proxyFactory.getProxy(invoker);
      
      // test integer
      int i = client.test(1);
      Assert.assertEquals(2, i);
      
      // test string and integer
      String result = client.test("haha",0);
      Assert.assertEquals("haha:1", result);
      
      // test timeout
      try {
      	client.testTimeout(1000);
      	Assert.fail("timeout doesn't work");
} catch (Exception e) {
	Assert.assertTrue( e instanceof RpcException);
}
      
      invoker.destroy();
      exporter.unexport();
      
      broker.stop();
  }