Spring Jms - Lost Context - spring

i'm writing webapp that uses few workers. I decide to use JMS ActiveMQ to communication between them. My problem is that when i change workers configuration to java (when config was in xml i couldn't run jar) i have problem when message returns to webapp :
[org.springframework.jms.listener.DefaultMessageListenerContainer#4-3] WARN o.s.j.l.DefaultMessageListenerContainer - Execution of JMS message listener failed, and no ErrorHandler has been set.
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Earlier i had configuration like this (in xml):
<jms:listener-container>
<jms:listener destination="sample.reply" ref="sampleMessageListener" />
</jms:listener-container>
All worked correctly and then in my workers i change it to config like this:
#Bean
public static DefaultMessageListenerContainer configureListener() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setDestinationName("xxxxx.xxxxx");
container.setMessageListener(context.getBean("someBean"));
container
.setConnectionFactory(context.getBean(ConnectionFactory.class));
container.setConcurrency("5-10");
return container;
}
This also works but when i send message back to webapp i see problem with lost context.

Related

Is it possible to generate a request scope?

I'm using a request scoped bean in my application:
#Bean
#RequestScope
public BeanA beanA(){
return new BeanA();
}
And, as long as I get web requests it is fine.
Problem is that I'm now supporting also other sources of request (for example Spring data redis listener) but, when I want to populate it with values I get
no thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Would it possible to create a request scope?

What's the correct exception type to NACK a message using annotation based listener in spring-boot with amqp?

I'm using spring boot with spring-amqp and annotation based listener to consume message from a rabbitmq broker.
I've a spring component which contains a method like this:
#RabbitListener(queues = "tasks")
public void receiveMessage(#Payload Task task) {...}
I'm using the AUTO mode to acknowledge messages after successful execution of receiveMessage(...). If i detect a special error, i'm throwing AmqpRejectAndDontRequeueException to get this message into a configured dead letter queue. Now i need to nack a message only, so that the message gets requeued into the main queue of rabbitmq and another consumer has the possibility to work on that message again.
Which exception should i throw for that? I wouldn't like to use channel.basicNack(...) like described here (http://docs.spring.io/spring-integration/reference/html/amqp.html) if possible.
As long as defaultRequeueRejected is true (the default) in the container factory, throwing any exception other than AmqpRejectAndDontRequeueException will cause the message to be rejected and requeued.
The exception must not have a AmqpRejectAndDontRequeueException in its cause chain (the container traverses the causes to ensure there is no such exception).

Servlet context and Session in Camel Spring remoting via JMS

I'm trying to integrate 2 spring-mvc applications using Camel-Spring-remoting via JMS/ActiveMQ component.
I'm successfully able to call a remote method in App1 from App2.
App1 and App2 has Shared Web Sessions (using Spring-Session/Redis) which has data like currentUserId and it's properties. The call the session something like below:
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attributes.getRequest().getSession(false);
session.getSession().getAttribute("SYSTEM_USER_ACCOUNT");
Because the remoting method calling is happening via JMS, App1 is not able to identify the previously authenticated HTTP session and I'm getting below exception in App1
org.apache.camel.RuntimeCamelException: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1556)
at org.apache.camel.component.bean.BeanInvocation.invoke(BeanInvocation.java:87)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:134)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
.
.
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at com.keype.hawk.core.api.system.util.SessionUtils.getSession(SessionUtils.java:38)
at com.keype.hawk.core.api.system.util.SessionUtils.getSystemUserAccount(SessionUtils.java:104)
at com.keype.hawk.core.api.system.util.SessionUtils.getStaffId(SessionUtils.java:98)
at com.keype.hawk.core.impl.party.service.PartyServiceImpl.preUpdate(PartyServiceImpl.java:208)
at com.keype.hawk.core.impl.party.service.PartyServiceImpl.updateParty(PartyServiceImpl.java:525)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
What's the practice here ? How can I access the servlet context and current user's http-session from within a JMS call ?

How to get request info on session created in Spring MVC?

I'm hoping to save some client info (IP address, etc) to a database on session created in Spring MVC.
I created a class implementing HttpSessionListener and configured it in web.xml. However, I'm not sure where to go after that.
Would like to be able to inject a bean as well (Spring Data JPA repository).
I've seen How to get the IP address when a session is created? , however if I try to access RequestContextHolder.currentRequestAttributes() I get the following exception:
SEVERE: Session event listener threw exception
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
You can create a bean this way with Spring java config:
#Bean
#Named ("IP")
#Scope ("session")
public String ip (HttpServletRequest request) {
return request.getRemoteAddr ();
}
If all you want to do is log stuff then you should use the HttpSessionListener, please provide your source and full stack trace. Use pastebin.com if necessary.

How to stop exception propagation if jms broker is down when sending a message with spring integration?

I configured spring xml based interceptor, which sends a jms message to activemq queue on each invokation of some transactional method after it is commited. It's happening with the following xml code.
<jms:outbound-channel-adapter channel="filteredStakesChannel" destination="stakesQueue" delivery-persistent="true" explicit-qos-enabled="true" />
But if the activemq server is down i get connection refused exception, which is propagated and i don't want this to happen even if the jms delivery fails. Is this possible?
Should i use some error-channel?
The simplest solution is to make fileredStakesChannel an Executor channel and the send will run on a different thread.
http://static.springsource.org/spring-integration/reference/html/messaging-channels-section.html#executor-channel
http://static.springsource.org/spring-integration/reference/html/messaging-channels-section.html#channel-configuration-executorchannel
Use the <task/> namespace to define an executor to use.

Resources