Java Spring send back message to queue from consumer
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
add a comment |
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
add a comment |
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
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
java spring rabbitmq
asked Nov 14 '18 at 18:42
yenkyenk
548
548
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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);
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 thefinal
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
add a comment |
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.
would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?
– yenk
Nov 14 '18 at 18:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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 thefinal
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
add a comment |
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);
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 thefinal
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
add a comment |
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);
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);
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 thefinal
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
add a comment |
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 thefinal
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
add a comment |
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.
would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?
– yenk
Nov 14 '18 at 18:53
add a comment |
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.
would it be better if I just call "MyService" in my "MyListener.receiveMessage()"?
– yenk
Nov 14 '18 at 18:53
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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