javaspring-jmsgrpc-javaprotobuf-javajmstemplate

java grpc server for a bi-directional stream that connect activemq to push & get message from activemq


I am trying to write a java grpc server for a bi-directional stream that connect activemq to push & get message from activemq

I am able to instantiate a Stream observer on the server. However the problem is once i get data from grpc client on invocation of the onNext function on the Server StreamObserver. I am trying to push the message in activemq using JMSTemplate .. I am getting jmsTemplate as null always .

I am configuring JMSTemplate as below to create JMSTemplate,

@Configuration
@EnableJms
public class JmsConfig {

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
            ConnectionFactory connectionFactory) {

        DefaultJmsListenerContainerFactory factory
                = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrency("5-10");

        return factory;
    }
}

application.yml

spring:
  application-name: bidirectional-server
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    packages.trust-all: true

HelloService.java

@GrpcService

public class HelloService extends HelloServiceGrpc.HelloServiceImplBase {

    /**
     * @param responseObserver
     */
    @Override
    public StreamObserver<HelloEnvelope> transfer(StreamObserver<HelloEnvelope> responseObserver) {
        return new TransferHelloRequestStreaming(responseObserver);
    }
}

TransferHelloRequestStreaming.java

@Slf4j
@Service
public class TransferHelloRequestStreaming implements StreamObserver<HelloEnvelope> {


    private  StreamObserver<HelloEnvelope> transferHelloResponseStreamObserver;

    private HelloEnvelope transferHelloResponse = null;
    public TransferMessageRequestStreaming() {
    }
    public TransferHelloRequestStreaming(StreamObserver<HelloEnvelope> transferHelloResponseStreamObserver) {
        this.transferHelloResponseStreamObserver = transferHelloResponseStreamObserver;
    }

    @Override
    public void onNext(HelloEnvelope transferHelloRequest) {

   
       new HelloProducer().sendTo("input",transferHelloRequest);

  
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onCompleted() {
     this.transferHelloResponseStreamObserver.onCompleted();
    }

}

HelloProducer.java

@Service
@Slf4j
public class HelloProducer {

  @Autowired
  JmsTemplate jmsTemplate;

  public void sendTo(String destination, MessageEnvelope transferHelloRequest) {
 
    jmsTemplate.convertAndSend(destination, transferHelloRequest);

  }
}

HelloService.proto

syntax = "proto3";

import "google/protobuf/any.proto";
option java_multiple_files = true;
option java_outer_classname = "HelloProto";
package com.server.grpcserver;

service HelloService {
  
    rpc transfer(stream HelloEnvelope) returns
        (stream HelloEnvelope) {
    }
}

message HelloEnvelope {
    map<string, google.protobuf.Any> header =1;
    bytes body =2;
}

Here in the HelloProducer.java always i always get jmsTemplate as null. Once i get data from grpc client in the onNext() , i want to push the data in activeMQ.


Solution

  • new HelloProducer()

    You need to @Autowire the HelloProducer as well - you are creating a new instance (that is not managed by Spring, so it doesn't get any auto wiring).