I am building spring boot app that will receive payload as PUB/SUB message on one topic, and return success/error message to other PUB/SUB topic.
This is config code:
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
//region Inbound Channel adapter
@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
@Qualifier("pubsubInputChannel") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate) {
PubSubInboundChannelAdapter adapter =
new PubSubInboundChannelAdapter(pubSubTemplate, "inboundSub");
adapter.setOutputChannel(inputChannel);
return adapter;
}
@Bean
public MessageChannel pubsubInputChannel() {
return new DirectChannel();
}
private static final Log LOGGER = LogFactory.getLog(PdfserviceApplication.class);
@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver() {
return message -> {
LOGGER.info("Message arrived! Payload: " + new String((byte[]) message.getPayload()));
GTService.sendMessage(new String((byte[]) message.getPayload()));
};
}
//endregion
//region outbound channel adapter
@Bean
@ServiceActivator(inputChannel = "pubsubOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
return new PubSubMessageHandler(pubsubTemplate, "outboundTopic");
}
@MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {
void sendToPubsub(String text);
}
//endregion
}
So, when message came to inboundTopic, my app is subscribed and relay that message to my GTService class that will have autowired MessagingGateway and simply return another message to outboundTopic.
GTService class:
public class GTService
{
@Autowired
private static PdfserviceApplication.PubsubOutboundGateway messagingGateway;
public static void sendMessage (String payload){
messagingGateway.sendToPubsub("I confirm that I received:" + payload );
}
}
So I expect when message arrive on inboundTopic, I will log it in local console and my class will send return message to outbound topic (I will check that in Google Console). The problem is when I enter new message via Google console to inboundTopic, the message is logged but it keeps repeating over and over again - like it is never acked. Also, no message has been sent to outboundTopic (I checked that in google console).
I am not sure what I am doing wrong. if anyone has any idea, I would be grateful.
GTService.messagingGateway
is almost definitely null
, so you are getting an NPE when sendMessage()
is invoked. There is likely some logging misconfiguration that hides error logging.
The reason for it is that PubsubOutboundGateway
is created during Spring's regular context initialization, which is long after static fields are initialized.
You will need to make messagingGateway
and sendMessage()
non-static, annotate GTService
as @Component
, and autowire the GTService
instance into TestApplication
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.