Mule Request Reply: Send Reply After All the Flow Processed - request

currently I am working with mule. I have 3 flow: RequestFlow, ServiceResponse, and SendResponse.
On the first flow, I processed the request (transform the request parameter, write it into wmq, etc). FYI, wmq on this flow can only be used for write.
On the second flow, I read the response from server via another wmq, transform it into json, and send to VM. FYI, wmq on this flow only can be used for read.
On the third flow, I tried to send back the response to the first flow and generate a file.
To send back the response from flow 3 to flow 1, I try to use request-reply
But, unfortunately, when I tried to send a request, I found out that:
After it reach request-reply component on the first flow, it will directly go to the third flow.
And then, after mule processed all the operation on the third flow, it will send the response back to the request-reply component.
Do some logging (logger component on first flow)
Then, go to the flow, processed all the operation
Processed the third flow again
That's why, after all the process has been finished, my application will:
Generate 2 files (1 contain request xml and 1 contain json response)
Return the request xml to http
However, It's not what I want. The flow that I need is:
Mule processed the operation on first flow until the request-reply component
Go to the second flow and processed all the component
After it finish with second flow, it will goes to third flow. Proceed all the component
Send the reply back to request-reply component on the first flow
Do some logging (logger component in first flow)
And finish
Result from this application should be:
1 File contain JSON response
JSON Response on http
So, how to do so? Thanks in advance.

You don't show the flow that consumes the messages sent to the sender path by the VM outbound endpoint in request-reply: I'm assuming it's a flow that takes care of sending the message to the server.
It seems that all you miss is an VM outbound endpoint in SendResponse that would send the message to the response path, onto which the VM inbound is waiting in the request-reply.
PS. Of course, it's assumed the the server propagates the JMS correlation ID from the request message to the response message, otherwise Mule (nor any client for that matter) could ever reconnect the response to the request and the request-reply would fail.
PPS. You don't need an all router around the single VM outbound endpoint in request-reply.

Related

Retrieving the response code or body to use as a condition to send another request within tryMax

I have this request that is querying my service which is inside of tryMax.
The access token to authenticate a request expires every five minutes and is generated at the beginning of the simulation run as ${token}
Is there a way within the tryMax to send another token generation request that will update the expired ${token} (Authorization header value) if the response code is 401 or the response body contains information about the request not being authenticated. Then retry the request before tryMax moves to the next iteration?
I have tried setting status code as a session attribute, however the request is not being sent and the token doesn't update, I tried doing a .doIf after the request exec, putting a doIf inside it's own exec and even playing around with transformResponse, all with no success.
Any suggestions how to approach this?
you can do something like what is outlined in
Gatling (performance test):how to perform task in background every x-minutes
However - is this really the scenario you want to model? How does the client you are simulating handle the 401? The scenario you are proposing only works if the client is in charge of manually handling its own refreshes.

Asynchronous request-response across two Netty channels in Camel

I have to integrate with a legacy host that uses TCP/IP communication with separate request and response channels. You send a request to the host on one channel where it is the server, and you need to have a server channel open on which it will send the response some time later. The communication is asynchronous, so there is no guarantee that the next message you receive will be the response to the request you just sent - you have to use a correlation key in the response to tie it back to the request.
I have a Camel route that takes the incoming request and sends it out to the host, and another route that listens for the responses. I have a third route that uses an aggregator to tie the response back to the request using a correlation key. Roughly speaking, the routes look like this:
from("direct:myService")
.process(exchange -> exchange.setProperty("CorrelationKey", exchange.getIn().getBody(MyMessage.class).getCorrelationKey())
.to("netty4:tcp://somehost:555")
.to("direct:aggregate");
from("netty4:tcp://localhost:555")
.process(exchange -> exchange.setProperty("CorrelationKey", exchange.getIn().getBody(MyResponse.class).getCorrelationKey())
.to("direct:aggregate");
from("direct:aggregate")
.aggregate(header("CorrelationKey"), (oldEx, newEx) -> {
if (oldEx == null) {
return newEx;
}
oldEx.getOut().setBody(newEx.getIn().getBody());
oldEx.setProperty(Exchange.AGGREGATION_COMPLETE_CURRENT_GROUP, true)
return oldEx;
}).completionTimeout(5000)
.process(exchange -> logger.log("Received response"));
The aggregation strategy works in the sense that I only see the log message once the response has been processed. The problem is that the request route ("direct:myService") doesn't wait for the aggregation - it has already returned back to the caller. What I want is for that route to block until the aggregation strategy has got the response, so that the message received by the netty4 consumer is used as the out message of the direct:myService route. Is that possible?

Convert email to http request with App Engine

Is there a way to send http request (to a specific url address) each time i receive email (Google account), with the content of the email received using Google App Engine?
As per your question, it seems that you already have an Incoming Email Handler in your App Engine application.
If the above is true, then in the Incoming Email Handler, you can parse out the message and if it meets your condition for invoking the http request, then you can definitely do that. You can use the URL Fetch service for the same.
One design decision you might want to do is whether you want to keep all your URL Fetch code inside of the incoming Email Handler or you want that to be handled externally via a Task Queue. In that case, I suggest that you can use the Task Queue to create a task when an incoming email comes in to your handler. Then the Task Queue logic can take care of one or more things, which includes invoking the HTTP Service, as you wanted.

Obtaining a previous message within an Apache Camel route

I'm pretty new to camel so perhaps I'm going about this the wrong way but I'm routing messages from one endpoint to another and transforming them on the way. However the next stage is to add authentication to the pipeline. I have a service that tracks authenticated users. My plan is to, in the first stage of the route, to add a filter that checks to see if the current user is authenticated. If the user is not I want to transform the message into an authentication request and send that to my endpoint. All good so far, however, after authentication (if successful) I want to send the original message down the pipeline. Is this something that can be done?
A simplified version of my route would be:
from("seda:in").
filter(method(Authentication.class, "isNotAuthenticated")).
bean(AuthenticationTransformer.class)
to("cxfbean:out")
.end()
.bean(RequestTransformer.class)
.to("cxfbean:out")
The same message would be sent to both transformer beans.
You should preserve the message in the Exchange property setProperty("originalMessage", body()) before transforming it. Afterwards you can access that property using getProperty("originalMessage")

Spring integration | How to preserve message context when using HTTP outbound gateway?

I need to POST a JSON structure to a REST endpoint and process the data it returns (all of this is with JSON).
I am planning to use a HTTP outbound gateway for this purpose. Now the thing is that after I have transformed my object (payload of the message) into a JSON format and before I transmit it to the endpoint the payload should be dropped into a database so that in case the endpoint is not available the call can be retried.
As I want to
a) set the status accordingly after the call`
b) update the
respective row with a uuid from the REST endpoint
I need to somehow relate the uuid from my object (the business key) as part of the outbound message to the response of the REST endpoint that is placed on the reply channel. As I cannot ask the provider to return my uuid as part of the response how can I achieve this purely on the client side?
You can add a custom advice to the outbound endpoint using the request-handler-advice-chain. Simply subclass AbstractRequestHandlerAdvice. It's effectively an 'around' advice so you can store it in the DB before invoking the handler and update the status afterwards.
See 'Adding Behavior to Endpoints'
and specifically 'Custom Advice Classes'

Resources