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

LocalRunningGrpcPort annotation does not work in main code #353

Open
Cypher01 opened this issue May 4, 2023 · 5 comments
Open

LocalRunningGrpcPort annotation does not work in main code #353

Cypher01 opened this issue May 4, 2023 · 5 comments

Comments

@Cypher01
Copy link

Cypher01 commented May 4, 2023

Using the LocalRunningGrpcPort annotation in test code works, the port is injected.

When using the annotation in main code, the injection does not work, null is returned.

The annotation worked in main code in version 4.x with Spring Boot 2.7.x.
The annotation does not work anymore in main code with version 5.1.1 with Spring Boot 3.0.6.

@Cypher01
Copy link
Author

Cypher01 commented May 4, 2023

I found out the cause of the problem.

The annotation is a wrapper @value("#{@gRpcServerProperties.getRunningPort()}"). GRpcServerProperties is autowired in this case. All of this works, but the procedure how GRpcServerProperties runningPort is instantiated changed. Version 4 instantiated the member inside the getter if it is null before returning it. Version 5 however instantiates the value using an EventListener annotated method listening for GRpcServerInitializedEvent. This event occurs after the value is injected by LocalRunningGrpcPort, which causes the getter to return null.

I don't know a solution for this. Maybe it is possible to wait for a specific event (GRpcServerInitializedEvent in this case) before the value is injected?

@jvmlet
Copy link
Collaborator

jvmlet commented May 5, 2023

This is about when you instantiate the instance of class that has this field. If you make this bean be dependent on grpc beans, it will be propagated correctly.

@Cypher01
Copy link
Author

Cypher01 commented May 5, 2023

I don't understand.
When you say "instance of class", which class do you mean? My class that uses LocalRunningGrpcPort annotation on its member?
When you say "make this bean be dependent on grpc beans", which bean do you mean by "this bean" and which beans by "grpc beans"?

Can you give an example?

For context, my class looks like this:

@Service
public class RegistrationService {
    @LocalRunningGrpcPort
    private int grpcPort;

    @EventListener(classes = ContextStartedEvent.class)
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public void register() {
        // Do something with grpcPort
    }
}

To explain the Order annotation, I use this because I listen for the ContextStartedEvent on multiple places and want to execute the register method first.

Maybe you can help me to pimp my code to not run into this problem.

@jvmlet
Copy link
Collaborator

jvmlet commented May 5, 2023

Ah,ok, for your use case it's better to use the event listener on started grpc server, you can get the port from there https://github.com/LogNet/grpc-spring-boot-starter#5-events

@Cypher01
Copy link
Author

Cypher01 commented May 5, 2023

I changed my implementation to simply listen to the GRpcServerInitializedEvent instead of ContextStartedEvent and get the port from the event. This works for me.

Still I would consider this a workaround. The original issue, the LocalRunningGrpcPort annotation not working, is not resolved. Everyone using this annotation can/will run into trouble. So let's not close this issue until a solution is found.

In the meantime everyone can use this workaround:

Change

public class YourClass {
    @LocalRunningGrpcPort
    private int grpcPort;

    // your code
}

to

public class YourClass {
    private int grpcPort;

    @EventListener
    public void setGrpcPort(GRpcServerInitializedEvent event) {
        grpcPort = event.getServer().getPort();
    }

    // your code
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants