Examples of RpcInvocation


Examples of com.alibaba.dubbo.rpc.RpcInvocation

    }

    private void encodeRequest( Channel channel, ChannelBuffer buffer, Request request )
            throws IOException {

        RpcInvocation inv = ( RpcInvocation ) request.getData();

        int seqId = nextSeqId();

        String serviceName = inv.getAttachment(Constants.INTERFACE_KEY);

        if ( StringUtils.isEmpty( serviceName ) ) {
            throw new IllegalArgumentException(
                    new StringBuilder( 32 )
                            .append( "Could not find service name in attachment with key " )
                            .append(Constants.INTERFACE_KEY)
                            .toString() );
        }

        TMessage message = new TMessage(
                inv.getMethodName(),
                TMessageType.CALL,
                seqId );

        String methodArgs = ExtensionLoader.getExtensionLoader( ClassNameGenerator.class )
            .getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME))
            .generateArgsClassName(serviceName, inv.getMethodName());

        if ( StringUtils.isEmpty( methodArgs ) ) {
            throw new RpcException( RpcException.SERIALIZATION_EXCEPTION,
                                    new StringBuilder(32).append(
                                            "Could not encode request, the specified interface may be incorrect." ).toString() );
        }

        Class<?> clazz = cachedClass.get( methodArgs );

        if ( clazz == null ) {

            try {

                clazz = ClassHelper.forNameWithThreadContextClassLoader( methodArgs );

                cachedClass.putIfAbsent( methodArgs, clazz );

            } catch ( ClassNotFoundException e ) {
                throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
            }

        }

        TBase args;

        try {
            args = (TBase) clazz.newInstance();
        } catch ( InstantiationException e ) {
            throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
        } catch ( IllegalAccessException e ) {
            throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
        }

        for( int i = 0; i < inv.getArguments().length; i++ ) {

            Object obj = inv.getArguments()[i];

            if ( obj == null ) { continue; }

            TFieldIdEnum field = args.fieldForId( i + 1 );

            String setMethodName = ThriftUtils.generateSetMethodName( field.getFieldName() );

            Method method;

            try {
                method = clazz.getMethod( setMethodName, inv.getParameterTypes()[i] );
            } catch ( NoSuchMethodException e ) {
                throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
            }

            try {
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

  @Test
  public void testAttachInvocationIdIfAsync_normal() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true");
    Map<String,String> attachments = new HashMap<String,String>();
    attachments.put("aa", "bb");
    Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}, attachments);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id1 = RpcUtils.getInvocationId(inv);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id2 = RpcUtils.getInvocationId(inv);
    Assert.assertTrue( id1 == id2); //幂等操作验证
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

   * 验证:acctachment中没有添加id属性
   */
  @Test
  public void testAttachInvocationIdIfAsync_sync() {
    URL url = URL.valueOf("dubbo://localhost/");
    Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    Assert.assertNull(RpcUtils.getInvocationId(inv));
  }
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

   * 验证:当原始acctachment为null时,不能报错.
   */
  @Test
  public void testAttachInvocationIdIfAsync_nullAttachments() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    Assert.assertTrue(RpcUtils.getInvocationId(inv) >= 0l);
  }
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

   * 验证:acctachment中没有添加id属性
   */
  @Test
  public void testAttachInvocationIdIfAsync_forceNotAttache() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true&"+Constants.AUTO_ATTACH_INVOCATIONID_KEY+"=false");
    Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    Assert.assertNull(RpcUtils.getInvocationId(inv));
  }
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

   * 验证:acctachment中有添加id属性
   */
  @Test
  public void testAttachInvocationIdIfAsync_forceAttache() {
    URL url = URL.valueOf("dubbo://localhost/?"+Constants.AUTO_ATTACH_INVOCATIONID_KEY+"=true");
    Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    Assert.assertNotNull(RpcUtils.getInvocationId(inv));
  }
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

                                    .append(" and your message type is ")
                                    .append(args[i].getClass().getName()).toString());
                        }
                    }
                }
                Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
                if (result.hasException()
                        && ! (result.getException() instanceof GenericException)) {
                    return new RpcResult(new GenericException(result.getException()));
                }
                if (ProtocolUtils.isJavaGenericSerialization(generic)) {
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String generic = invoker.getUrl().getParameter(Constants.GENERIC_KEY);
        if (ProtocolUtils.isGeneric(generic)
                && ! Constants.$INVOKE.equals(invocation.getMethodName())
                && invocation instanceof RpcInvocation) {
            RpcInvocation invocation2 = (RpcInvocation) invocation;
            String methodName = invocation2.getMethodName();
            Class<?>[] parameterTypes = invocation2.getParameterTypes();
            Object[] arguments = invocation2.getArguments();
           
            String[] types = new String[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i ++) {
                types[i] = ReflectUtils.getName(parameterTypes[i]);
            }

            Object[] args;
            if (ProtocolUtils.isBeanGenericSerialization(generic)) {
                args = new Object[arguments.length];
                for(int i = 0; i < arguments.length; i++) {
                    args[i] = JavaBeanSerializeUtil.serialize(arguments[i], JavaBeanAccessor.METHOD);
                }
            } else {
                args = PojoUtils.generalize(arguments);
            }
           
            invocation2.setMethodName(Constants.$INVOKE);
            invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES);
            invocation2.setArguments(new Object[] {methodName, types, args});
            Result result = invoker.invoke(invocation2);
           
            if (! result.hasException()) {
                Object value = result.getValue();
                try {
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

        Logger logger = EasyMock.createMock(Logger.class);
        RpcContext.getContext().setRemoteAddress("127.0.0.1", 1234);
        RpcException exception = new RpcException("TestRpcException");
        logger.error(EasyMock.eq("Got unchecked and undeclared exception which called by 127.0.0.1. service: " + DemoService.class.getName() + ", method: sayHello, exception: " + RpcException.class.getName() + ": TestRpcException"), EasyMock.eq(exception));
        ExceptionFilter exceptionFilter = new ExceptionFilter(logger);
        RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
        Invoker<DemoService> invoker = EasyMock.createMock(Invoker.class);
        EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class);
        EasyMock.expect(invoker.invoke(EasyMock.eq(invocation))).andThrow(exception);
       
        EasyMock.replay(logger, invoker);
View Full Code Here

Examples of com.alibaba.dubbo.rpc.RpcInvocation

        Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => false"));
        List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
        invokers.add(new MockInvoker<String>());
        invokers.add(new MockInvoker<String>());
        invokers.add(new MockInvoker<String>());
        List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
        Assert.assertEquals(0, fileredInvokers.size());
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.