How to integrate spring-integration in an existing spring-mvc application? - spring-mvc

i have a typical spring-webmvc application (with REST request mapped in Controller's method that call a Service's method) and I would like to use spring-integration to manage the asynchronous functionality (e.g. mail, sms, notification...).
For example i would like that a Service Component after insert in my DB publish a message in spring-integration context channel in asycn mode so that the control return immediataly at the Controller to return an http response to the client.
How to integrate "spring-integration" in my existing spring-mvc application?

Simply inject a Messaging Gateway into your controller. Define an interface, declare an <int:gateway id="toMail"/> and use normal Spring bean injection techniques to inject the gateway (as an instance of your interface) into the controller.
If you don't want to wait for a a reply; set the method return value to void and, if you make the first channel (the gateway's default-request-channel) an Executor Channel, the message will be handed off to another thread and the gateway will returm immediately.
Configuring an Executor Channel.

Related

Dagger hilt with room and job scheduler

I want to locally store data in absence of internet connection and thus am using job scheduler to schedule my syncing. So my service needs access to dao and I am not sure what the correct components needs to be defined for dagger to correctly inject dao inside my service. I do not know how to constructor inject in service either. I think it should not be constructor injected into the service. What is the proper approach? And lastly, which coroutine scope should i be using to access database from service? I also need retrofit api to make network calls. How should i inject them into my jobservice?

Is it possible to create a global context available throughout the web application when i receive a message from Azure Service Bus?

I have a web api application which subscribes to a topic in azure service bus during startup.
When I receive a message from service bus, is it possible to establish a context similar to HttpContext that is available across the application methods?
There's not enough here to give you any exact direction, but in general, this just involves scopes. If whatever is listening for messages is has a "scoped" lifetime, then you can inject your context directly. If, as is more likely, it has a singleton lifetime, then you must instead inject IServiceProvider and retrieve your context via:
using (var scope = provider.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<MyContext>();
// do something with context
}
You can only use the context within the scope, so do not try to do something like store it in an instance variable or something.

With CDI, for a statefull EJB without a view, what scope should I use?

I must create a statefull service, with stateful EJB, but without any view (so, no http request, no http session). This service is going to be used via direct API call only.
I would like to be able to inject objets that matches the life cycle of the stateful EJB (for exemple, the user call a 'login' method of the EJB, and I would like to be able to inject the username in the beans used in subsequent calls to the service).
Am I supposed to create my own custom Scope, or, is the use case 'view-less statefull EJB scope' somehow already handled by CDI ?

Invoking Spring MVC controller method from another thread (javax.jms.MessageListener)

I'm looking for a way of sending messages to my Spring MVC Controller from another thread. In particular my thread is an implementation of javax.jms.MessageListener which listens for messages on a ActiveMQ queue. As soon as I get a new message in the Queue, the jms MessageListener's onMessage() method is invoked. However, now I want to invoke another method in my spring controller whenever MessageListener's onMessage() is invoked.
The controllers in Spring MVC are not intended to be directly invoked the way you are looking for, but are designed to handle and respond to to web requests. A cleaner way to handle what you are doing will be to move the controller logic that you plan to invoke to a services layer and invoke this common services layer from controller and JMS listener.
If you absolutely want to, you can always autowire in a controller and invoke methods on it as if it is a normal POJO though.

How can I Manually create a client proxy for a WF4 (xamlX) Service

I've created several services by wrapping the WorkflowServiceHost in a WCF service; using WorkflowHostingEndpoint. Doing this I was able to define my service contract and create proxy classes to connect to those services.
I'm now creating a service in which I want to use the WF4 messaging activities and again self host the service. I also REALLY want to manually create my proxy classes without using the ServiceReference in VS2010 or ServiceUtil...
I've seen some references that use the Send Activity in the Service client but I'd like to be able to use a more "WCF-like" proxy created directly against the service contract like I've done with the other services. How can I do that? Is there anywhere a xamlX (or xaml with messaging activities) stores the interface contract that I can use to generate a proxy manually?
NOTE: I don't want to use the Send activity as described in the WF_WCF_Samples.
UPDATE:
I tried creating an interface identical to the workflow receive activity before posting this question; but I keep getting the following error:
The message with Action 'http://tempuri.org/ISvrClientService/Create'
cannot be processed at the receiver, due to a ContractFilter mismatch
at the EndpointDispatcher. This may be because of either a contract
mismatch (mismatched Actions between sender and receiver) or a
binding/security mismatch between the sender and the receiver. Check
that sender and receiver have the same contract and the same binding
(including security requirements, e.g. Message, Transport, None).
Thanks!
There is no need to use the Send activity. When you host a workflow service you are hosting a SOAP endpoint, the only difference is the implementation but that is a private detail. You can create a proxy object using ChannelFactory just like with any other WCF service.
var factory = new ChannelFactory<IYourService>();
var proxy = factory.CreateChannel();
The IYourService interface is somethign you have to hand craft. There isn't one on the server, it is done in a workflow, so you have to code up the identical contract.
I've done this successfully with channel factory and manually created interfaces. The trick was in matching-up the reply/send in/out parameters names not just type signature.
This is particularly crucial if you have bookmarks with correlation in your workflow.

Resources