micronaut-projects / micronaut-nats

Integration between Micronaut and Nats.io (https://nats.io/)
Apache License 2.0
14 stars 9 forks source link

Listener not receiving messages #32

Closed JamieCressey closed 4 years ago

JamieCressey commented 4 years ago

I have a publisher which looks like;

@NatsClient
public interface InternalEventService {
    @Subject("TopicName")
    void send(@Body InternalEvent<?> event);
}

By enabling debug logging I can see messages successfully being published. Attaching a python consumer to the topic also validates messages are correctly being published and can be consumed.

Python sample code;

#!/usr/bin/env python3

import asyncio
from nats.aio.client import Client as NATS

async def main():
    nc = NATS()

    await nc.connect(servers=["nats://localhost:4222"])

    future = asyncio.Future()

    async def cb(msg):
      nonlocal future
      future.set_result(msg)

    await nc.subscribe("TopicName", queue="workers", cb=cb)

    msg = await asyncio.wait_for(future, 10000000)
    print("Msg", msg)

However, I do not receive any messages when I attempt to consumer them in Micronaut using the following code;

@NatsListener
@Slf4j
public class MessageListener {
    @Subject("TopicName")
    private void schoolEvent(String event) {
        log.info("New event: {}", event);
    }
}

With debug logging enabled I do not see the MessageListener class being created. Annotating the class with @Context creates an instance of the class, but still nothing is received.

My Maven build config was auto-generated by the Micronaut CLI and looks like this;

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${exec.mainClass}</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>-noverify</argument>
                        <argument>-XX:TieredStopAtLevel=1</argument>
                        <argument>-Dcom.sun.management.jmxremote</argument>
                        <argument>${exec.mainClass}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                    <configuration>
                        <detail>true</detail>
                        <includes>
                            <include>%regex[.*]</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>${maven-failsafe-plugin.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                            <path>
                                <groupId>io.micronaut</groupId>
                                <artifactId>micronaut-inject-java</artifactId>
                                <version>2.0.0.M3</version>
                            </path>
                            <path>
                                <groupId>io.micronaut</groupId>
                                <artifactId>micronaut-validation</artifactId>
                                <version>2.0.0.M3</version>
                            </path>
                            <path>
                                <groupId>io.micronaut.configuration</groupId>
                                <artifactId>micronaut-openapi</artifactId>
                                <version>1.5.1</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                    <executions>
                        <execution>
                            <id>test-compile</id>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                            <configuration>
                                <compilerArgs>
                                    <arg>-parameters</arg>
                                </compilerArgs>
                                <annotationProcessorPaths>
                                    <path>
                                        <groupId>org.projectlombok</groupId>
                                        <artifactId>lombok</artifactId>
                                        <version>${lombok.version}</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut</groupId>
                                        <artifactId>micronaut-inject-java</artifactId>
                                        <version>2.0.0.M3</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut</groupId>
                                        <artifactId>micronaut-validation</artifactId>
                                        <version>2.0.0.M3</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut.configuration</groupId>
                                        <artifactId>micronaut-openapi</artifactId>
                                        <version>1.5.1</version>
                                    </path>
                                </annotationProcessorPaths>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
grimmjo commented 4 years ago

Hi @JamieCressey, I'm will have a look at this issue. Can you maybe provide me a little example project?

JamieCressey commented 4 years ago

I managed to figure it out. The consumer was defined as a private method, making it public fixed the problem.

From;

@Subject("TopicName")
    private void schoolEvent(String event) {
        log.info("New event: {}", event);
    }

To;

@Subject("TopicName")
    public void schoolEvent(String event) {
        log.info("New event: {}", event);
    }