Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Kotlin Coroutine based client stubs #437

Closed
imberda opened this issue Oct 10, 2020 · 1 comment
Closed

Add support for Kotlin Coroutine based client stubs #437

imberda opened this issue Oct 10, 2020 · 1 comment
Labels
enhancement A feature request or improvement

Comments

@imberda
Copy link

imberda commented Oct 10, 2020

The problem

This project already supports the creation of different GRPC stub types (Blocking, Future, etc.). However GRPC Kotlin is and will be increasingly popular. When GRPC code is generated with grpc-kotlin the client stub extends AbstractCoroutineStub. It's currently not possible to instantiate instances of these stubs with this project.

The solution

The suggested solution is:

  1. Create an optional dependency on io.grpc:grpc-kotlin-stub:0.2.0

  2. Create a new stub factory - something like

    public class CoroutineStubFactory implements StubFactory {
    
        @Override
        public AbstractStub<?> createStub(Class<? extends AbstractStub<?>> stubType, Channel channel) {
    
            try {
                final Constructor<? extends AbstractStub<?>> constructor = stubType.getConstructor(Channel.class, CallOptions.class);
                return constructor.newInstance(channel, CallOptions.DEFAULT);
            } catch (Exception e) {
                throw new BeanInstantiationException(stubType, "Failed to create gRPC client via CoroutineStubFactory", e);
            }
        }
    
        @Override
        public boolean isApplicable(Class<? extends AbstractStub<?>> stubType) {
            return AbstractCoroutineStub.class.isAssignableFrom(stubType);
        }
    }
    
  3. Add an auto-configuration @Bean of CoroutineStubFactory guarded by @ConditionalOnClass(AbstractCoroutineStub.class)

NOTE: That unlike the other stubs already supported, Kotlin stubs that extend AbstractCoroutineStub are generated to use constructor based instantiation, as opposed to be being instantiated via a static method.

@imberda imberda added the enhancement A feature request or improvement label Oct 10, 2020
@ST-DDT ST-DDT added this to the 2.11.0 milestone Oct 10, 2020
@asarkar
Copy link

asarkar commented Oct 11, 2020

@imberda support for grpc-kotlin stubs was added via https://github.com/yidongnan/grpc-spring-boot-starter/issues/410. I’m using this library with grpc-kotlin successfully.

@imberda imberda closed this as completed Oct 11, 2020
@ST-DDT ST-DDT removed this from the 2.11.0 milestone Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature request or improvement
Projects
None yet
Development

No branches or pull requests

3 participants