Java Spring send back message to queue from consumer










3















I have a service that sends message to rabbitmq and the consumer do some manipulation of the message and re-queue them.



I can successfully send to rabbitmq the initial message but the problem is i cannot resend to rabbitmq any consumed message if the message requires modifications.



@Service
public class MyService

/**
* The template
*/
@Autowired
private AmqpTemplate amqpTemplate;
private final RabbitMQConfig config;

public void send(String message)
try
amqpTemplate.convertAndSend("ex", "r", message);

catch (Exception e)
e.printStackTrace();





Then in my config i have setup:
@Bean
public ConnectionFactory connectionFactory() /* working code */



@Bean
public Queue myQueue() { return new Queue("my-queue");
// etc...

@Bean
MessageListenerAdapter myListenerAdapter(MyListener listener)
return new MessageListenerAdapter(listener, "listener");


@Bean
MyListener myListener()
return new MyListener();



then...



public class MyListener 
public void receiveMessage(String message)
// ... some code
// if message requires modification, then repush
new Repush().push(message);




I tried to create a new class with new but the "myService" always null



@Component
public class Repush
@Autowired
private MyService myService;

public void push(String message)
// myService is null at this point











share|improve this question


























    3















    I have a service that sends message to rabbitmq and the consumer do some manipulation of the message and re-queue them.



    I can successfully send to rabbitmq the initial message but the problem is i cannot resend to rabbitmq any consumed message if the message requires modifications.



    @Service
    public class MyService

    /**
    * The template
    */
    @Autowired
    private AmqpTemplate amqpTemplate;
    private final RabbitMQConfig config;

    public void send(String message)
    try
    amqpTemplate.convertAndSend("ex", "r", message);

    catch (Exception e)
    e.printStackTrace();





    Then in my config i have setup:
    @Bean
    public ConnectionFactory connectionFactory() /* working code */



    @Bean
    public Queue myQueue() { return new Queue("my-queue");
    // etc...

    @Bean
    MessageListenerAdapter myListenerAdapter(MyListener listener)
    return new MessageListenerAdapter(listener, "listener");


    @Bean
    MyListener myListener()
    return new MyListener();



    then...



    public class MyListener 
    public void receiveMessage(String message)
    // ... some code
    // if message requires modification, then repush
    new Repush().push(message);




    I tried to create a new class with new but the "myService" always null



    @Component
    public class Repush
    @Autowired
    private MyService myService;

    public void push(String message)
    // myService is null at this point











    share|improve this question
























      3












      3








      3








      I have a service that sends message to rabbitmq and the consumer do some manipulation of the message and re-queue them.



      I can successfully send to rabbitmq the initial message but the problem is i cannot resend to rabbitmq any consumed message if the message requires modifications.



      @Service
      public class MyService

      /**
      * The template
      */
      @Autowired
      private AmqpTemplate amqpTemplate;
      private final RabbitMQConfig config;

      public void send(String message)
      try
      amqpTemplate.convertAndSend("ex", "r", message);

      catch (Exception e)
      e.printStackTrace();





      Then in my config i have setup:
      @Bean
      public ConnectionFactory connectionFactory() /* working code */



      @Bean
      public Queue myQueue() { return new Queue("my-queue");
      // etc...

      @Bean
      MessageListenerAdapter myListenerAdapter(MyListener listener)
      return new MessageListenerAdapter(listener, "listener");


      @Bean
      MyListener myListener()
      return new MyListener();



      then...



      public class MyListener 
      public void receiveMessage(String message)
      // ... some code
      // if message requires modification, then repush
      new Repush().push(message);




      I tried to create a new class with new but the "myService" always null



      @Component
      public class Repush
      @Autowired
      private MyService myService;

      public void push(String message)
      // myService is null at this point











      share|improve this question














      I have a service that sends message to rabbitmq and the consumer do some manipulation of the message and re-queue them.



      I can successfully send to rabbitmq the initial message but the problem is i cannot resend to rabbitmq any consumed message if the message requires modifications.



      @Service
      public class MyService

      /**
      * The template
      */
      @Autowired
      private AmqpTemplate amqpTemplate;
      private final RabbitMQConfig config;

      public void send(String message)
      try
      amqpTemplate.convertAndSend("ex", "r", message);

      catch (Exception e)
      e.printStackTrace();





      Then in my config i have setup:
      @Bean
      public ConnectionFactory connectionFactory() /* working code */



      @Bean
      public Queue myQueue() { return new Queue("my-queue");
      // etc...

      @Bean
      MessageListenerAdapter myListenerAdapter(MyListener listener)
      return new MessageListenerAdapter(listener, "listener");


      @Bean
      MyListener myListener()
      return new MyListener();



      then...



      public class MyListener 
      public void receiveMessage(String message)
      // ... some code
      // if message requires modification, then repush
      new Repush().push(message);




      I tried to create a new class with new but the "myService" always null



      @Component
      public class Repush
      @Autowired
      private MyService myService;

      public void push(String message)
      // myService is null at this point








      java spring rabbitmq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:42









      yenkyenk

      548




      548






















          2 Answers
          2






          active

          oldest

          votes


















          2














          Don't use new for bean creation. Spring injects fields only in beans. Your MyListener is a bean. Just add Repush field with @Autowired annotation in this class.



          public class MyListener 
          @Autowired
          private Repush repush;

          public void receiveMessage(String message)
          // ... some code
          // if message requires modification, then repush
          repush.push(message);







          share|improve this answer

























          • It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

            – Rad
            Nov 14 '18 at 19:01






          • 1





            it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

            – Rad
            Nov 14 '18 at 19:06



















          0














          If you declare myService as a bean in the application context as well as Repush as a bean you can then inject it into MyListener using @Autowired.



          By creating Repush using new at point-in-time within the listener method, you are not getting a bean that is cognizant of the context you are in.






          share|improve this answer























          • would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

            – yenk
            Nov 14 '18 at 18:53










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306825%2fjava-spring-send-back-message-to-queue-from-consumer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Don't use new for bean creation. Spring injects fields only in beans. Your MyListener is a bean. Just add Repush field with @Autowired annotation in this class.



          public class MyListener 
          @Autowired
          private Repush repush;

          public void receiveMessage(String message)
          // ... some code
          // if message requires modification, then repush
          repush.push(message);







          share|improve this answer

























          • It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

            – Rad
            Nov 14 '18 at 19:01






          • 1





            it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

            – Rad
            Nov 14 '18 at 19:06
















          2














          Don't use new for bean creation. Spring injects fields only in beans. Your MyListener is a bean. Just add Repush field with @Autowired annotation in this class.



          public class MyListener 
          @Autowired
          private Repush repush;

          public void receiveMessage(String message)
          // ... some code
          // if message requires modification, then repush
          repush.push(message);







          share|improve this answer

























          • It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

            – Rad
            Nov 14 '18 at 19:01






          • 1





            it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

            – Rad
            Nov 14 '18 at 19:06














          2












          2








          2







          Don't use new for bean creation. Spring injects fields only in beans. Your MyListener is a bean. Just add Repush field with @Autowired annotation in this class.



          public class MyListener 
          @Autowired
          private Repush repush;

          public void receiveMessage(String message)
          // ... some code
          // if message requires modification, then repush
          repush.push(message);







          share|improve this answer















          Don't use new for bean creation. Spring injects fields only in beans. Your MyListener is a bean. Just add Repush field with @Autowired annotation in this class.



          public class MyListener 
          @Autowired
          private Repush repush;

          public void receiveMessage(String message)
          // ... some code
          // if message requires modification, then repush
          repush.push(message);








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 19:12

























          answered Nov 14 '18 at 18:52









          Демьян БельскийДемьян Бельский

          1364




          1364












          • It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

            – Rad
            Nov 14 '18 at 19:01






          • 1





            it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

            – Rad
            Nov 14 '18 at 19:06


















          • It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

            – Rad
            Nov 14 '18 at 19:01






          • 1





            it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

            – Rad
            Nov 14 '18 at 19:06

















          It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

          – Rad
          Nov 14 '18 at 19:01





          It's more than just field injection. The author needs to learn the whole concept of spring, bean, etc.

          – Rad
          Nov 14 '18 at 19:01




          1




          1





          it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

          – Rad
          Nov 14 '18 at 19:06






          it's not compile-able BTW. Either remove the final keyword or add the constructor and if you add the constructor, Spring is smart enough to notice the dependency, so no need for @Autowired :)

          – Rad
          Nov 14 '18 at 19:06














          0














          If you declare myService as a bean in the application context as well as Repush as a bean you can then inject it into MyListener using @Autowired.



          By creating Repush using new at point-in-time within the listener method, you are not getting a bean that is cognizant of the context you are in.






          share|improve this answer























          • would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

            – yenk
            Nov 14 '18 at 18:53















          0














          If you declare myService as a bean in the application context as well as Repush as a bean you can then inject it into MyListener using @Autowired.



          By creating Repush using new at point-in-time within the listener method, you are not getting a bean that is cognizant of the context you are in.






          share|improve this answer























          • would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

            – yenk
            Nov 14 '18 at 18:53













          0












          0








          0







          If you declare myService as a bean in the application context as well as Repush as a bean you can then inject it into MyListener using @Autowired.



          By creating Repush using new at point-in-time within the listener method, you are not getting a bean that is cognizant of the context you are in.






          share|improve this answer













          If you declare myService as a bean in the application context as well as Repush as a bean you can then inject it into MyListener using @Autowired.



          By creating Repush using new at point-in-time within the listener method, you are not getting a bean that is cognizant of the context you are in.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 18:47









          Dave GDave G

          8,0782839




          8,0782839












          • would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

            – yenk
            Nov 14 '18 at 18:53

















          • would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

            – yenk
            Nov 14 '18 at 18:53
















          would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

          – yenk
          Nov 14 '18 at 18:53





          would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?

          – yenk
          Nov 14 '18 at 18:53

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306825%2fjava-spring-send-back-message-to-queue-from-consumer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo