HTTP client with spring-integration - spring-integration

I need to write a simple HTTP client to make simple GET request and get JSON response using Spring integration.
Call fails with no message in exception: org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error.
I tried debugging Spring code and did it successfully till I have source code, namely till
in the method AbstractMessageHandler.handleMessage(Message message)
abstract handleMessageInternal(Message message) has been called which threw
exception saying that request with
URL = http://example.com?q={q}&authKey={authKey}&rows={rows}&page={page}&filter={filter}
failed. URL looked exactly as I quoted, i.e. expressions have not been executed.
Payload in the message was always as it should be - instance if ZtInput with correct field values.
Could anyone give me an idea what to do?
Here is spring-integration-zt-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<int:channel id="InChannelZt"></int:channel>
<int:channel id="OutChannelZt"></int:channel>
<!-- Gateway Start -->
<int:gateway id="ZtGateway" default-request-timeout="5000" default-reply-timeout="5000"
default-request-channel="InChannelZt" service-interface="com.example.service.ZtService">
<int:method name="getResults" request-channel="InChannelZt" reply-channel="OutChannelZt" />
</int:gateway>
<int-http:outbound-gateway id="locationZtGateway"
request-channel="InChannelZt"
reply-channel="OutChannelZt"
url="${zt_url}?q={q}&authKey={authKey}&rows={rows}&page={page}&filter={filter}"
http-method="GET"
reply-timeout='5000'
expected-response-type="com.example.vo.ZtResponse">
<int-http:uri-variable name="q" expression="payload.getQ()"/>
<int-http:uri-variable name="authKey" expression="payload.getAuthKey()"/>
<int-http:uri-variable name="rows" expression="payload.getRows()"/>
<int-http:uri-variable name="page" expression="payload.getPage()"/>
<int-http:uri-variable name="filter" expression="payload.getFilter()"/>
</int-http:outbound-gateway>
and two classes mentioned in it:
import com.xxxx.vo.ZtInput;
import com.xxxx.vo.ZtResponse;
public interface ZtService {
ZtResponse getSearchResults(ZtInput ztInput);
}
Payload:
public class ZtInput {
private String q; //=pink
private String authKey = "baef7f8e39c53f852c8a14b7f6018b58";
private String rows="20";
private String page="1";
private String filter = "";
public ZtInputVO() {
}
public String getQ() {
return q;
}
public void setQ(String q) {
this.q = q;
}
public String getAuthKey() {
return authKey;
}
public void setAuthKey(String authKey) {
this.authKey = authKey;
}
public String getRows() {
return rows;
}
public void setRows(String rows) {
this.rows = rows;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
}

The URI in the exception is the original (unexpanded URI); the expansion is performed into a different variable. (We should/will change that to log the expanded URI). But the bottom line is your server didn't like the expanded URI and returned a 500 internal server error.
You can use a network/tcp monitor (eclipse has one built in or you can use wireshark) to examine the actual URL sent to the server. You can also look at the server logs, if enabled.
Or, in the debugger, step down to line 415 (in the current source code - version 4.0.4) and examine realUri.
EDIT: The exception now includes the expanded URI (currently available in 4.0.5.BUILD-SNAPSHOT and 4.1.0.BUILD-SNAPSHOT).

Related

Getting an Array from a SOAP XML response in NodeJS

I'm working on a SOAP client in NodeJS. I've managed to consume using SOAPUi,the thing is that the response for the request I'm getting has an Array inside the body.
The XML request looks like this:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xe="http://www.w3.org/2001/04/xmlenc#">
<soap:Header>
<xe:AuthenticationHeader>
<!--Optional:-->
<xe:UserName>user</xe:UserName>
<!--Optional:-->
<xe:Password>password</xe:Password>
</xe:AuthenticationHeader>
</soap:Header>
<soap:Body>
<xe:GetRegistration>
<!--Optional:-->
<xe:id>123456</xe:id>
</xe:GetRegistration>
</soap:Body>
</soap:Envelope>
And the response XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetRegistrationResponse xmlns="http://www.w3.org/2001/04/xmlenc#">
<GetRegistrationResult>
<ErrorInQuery>false</ErrorInQuery>
<ErrorInfo>Success!</ErrorInfo>
<ArrayRegistration />
</GetRegistrationResult>
</GetRegistrationResponse>
</soap:Body>
</soap:Envelope>
What I'm trying to get from the response is that ArrayRegistration which will be processed by a decrypting process to give me the plain text results.
I managed to do it on C# but since the proyect needs to be done in NodeJS I have been struggling with it.
This is the code that worked for me in C#
public partial class RRegistration : object, System.ComponentModel.INotifyPropertyChanged {
private bool errorQuery;
private string errorInformation;
private Registration[] arrayRegistrationField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public bool ErrorInQuery {
get {
return this.errorQuery;
}
set {
this.errorQuery = value;
this.RaisePropertyChanged("ErrorQuery");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public string ErrorInformation {
get {
return this.errorInformation;
}
set {
this.errorInformation = value;
this.RaisePropertyChanged("ErrorInformation");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order=2)]
public Registration[] ArrayRegistration {
get {
return this.arrayRegistration;
}
set {
this.arrayRegistration = value;
this.RaisePropertyChanged("ArrayRegistration");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
Honestly any help would be greatly appreciated, in NodeJS I've used Node-Soap and easy-soap-request but I'm not sure how to get the Array that I need to send to the decrypting method.
At the end the xml that I was sending was missing the xe: notation in each of the custom tags.

How to have Spring ContextConfiguration load both from XML and from JavaConfig

From what I've read of the Spring #ContextConfiguration annotation, it's possible to load multiple XML context files, or multiple JavaConfig classes. What I need is to load from one XML context file and one class. All the examples I've seen either load all XML, or all classes, but not both.
I'm trying to do this because I want my test class, which is just there to verify expected Spring wiring, to load my default applicationContext.xml file (presently just a copy stored in "src/test/resources, and trying to figure out how to directly specify the default one) along with a JavaConfig class that specifies some JNDI resources that need to be available. For the purposes of my test, I only need to set those JNDI resources to dummy strings, but I'd really like to specify them in an inline static class instead of an external XML file, because my tests are going to have to verify that some settings are equal to those dummy strings, and it's more maintainable if both the values and the checks are in the same file.
What I have so far, and what I've tried, can be illustrated with this:
#RunWith(SpringRunner.class)
#ContextConfiguration(value = {"/testApplicationContext.xml", "/testResources.xml"})
//#ContextHierarchy({
// #ContextConfiguration("/testApplicationContext.xml"),
// #ContextConfiguration(classes = SpringWiringTest.Config.class)
//})
#TestPropertySource(properties = { "env = tomcat", "doNotifications = false" })
public class SpringWiringTest {
And this at the end of the class:
#Configuration
public static class Config {
#Bean public String uslDatasourcesList() { return "abc"; }
#Bean public String atgDatasourcesList() { return "abc"; }
#Bean public String uslTableNamePrefixsList() { return "abc"; }
#Bean public String atgTableNamePrefixsList() { return "abc"; }
#Bean public String doNotifications() { return "false"; }
#Bean public DataSource abc() { return new DriverManagerDataSource(); }
}
If I comment out the first #ContextConfiguration and comment back in the #ContextHierarchy block, I get an error like this:
Error creating bean with name 'uslDatasourcesList': Invocation of init
method failed; nested exception is
javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an
application resource file: java.naming.factory.initial
Update:
Using the guideline of picking either JavaConfig or XML as the "entry point" to configuration, here are some modified excerpts that show what I'm trying:
#RunWith(SpringRunner.class)
#ContextConfiguration
//#ContextConfiguration(value = {"file:src/main/webapp/WEB-INF/applicationContext.xml", "/testResources.xml"})
#TestPropertySource(properties = { "env = tomcat", "doNotifications = false" })
public class SpringWiringTest {
...
#BeforeClass
public static void setup() throws Exception {
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
DataSource ds = new DriverManagerDataSource();
builder.bind("java:comp/env/abc", ds);
}
...
#Configuration
#ImportResource("file:src/main/webapp/WEB-INF/applicationContext.xml")
public static class Config {
#Bean public String uslDatasourcesList() { return "abc"; }
#Bean public String atgDatasourcesList() { return "abc"; }
#Bean public String uslTableNamePrefixsList() { return "abc"; }
#Bean public String atgTableNamePrefixsList() { return "abc"; }
#Bean public String doNotifications() { return "false"; }
#Bean public DataSource abc() { return new DriverManagerDataSource(); }
}
}
When I run my test, the bottom "Caused by" in the exception says this:
Caused by: javax.naming.NameNotFoundException: Name
[uslDatasourcesList] not bound; 1 bindings: [java:comp/env/abc]
In the alternate version, using the commented-out "#ContextConfiguration" (and commenting out the Config class and its annotations), this error does not occur.
Note that this the meat of my "testResources.xml" file:
<bean id="uslDatasourcesList" class="java.lang.String"> <constructor-arg value="abc"/> </bean>
<bean id="atgDatasourcesList" class="java.lang.String"> <constructor-arg value="abc"/> </bean>
<bean id="uslTableNamePrefixList" class="java.lang.String"> <constructor-arg value="abc"/> </bean>
<bean id="atgTableNamePrefixList" class="java.lang.String"> <constructor-arg value="abc"/> </bean>
<bean id="doNotifications" class="java.lang.String"> <constructor-arg value="false"/> </bean>
<bean id="abc" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>
Note that the bean mentioned in the error message, "uslDatasourcesList" is defined in both versions, but it's not working in the version with JavaConfig and XML mixed.
It almost appears that the beans in the "#ImportResource" annotation are evaluated on their own, before the beans declared in the JavaConfig class are merged into it.
This is clearly documented in the Spring Reference Manual in the section named Mixing XML, Groovy scripts, and annotated classes.
In summary, ...
... you will have to pick one as the entry point, and that one will have to include or import the other.
Thus, the following should hopefully solve your problem.
#RunWith(SpringRunner.class)
#ContextConfiguration
#TestPropertySource(properties = { "env = tomcat", "doNotifications = false" })
public class SpringWiringTest {
// ...
#Configuration
#ImportResource({"/testApplicationContext.xml", "/testResources.xml"})
static class Config {
// ...
}
}

AMQP pollable channel not recognised as pollable

My Spring Integration flow is defined in xml as per below (note that I have removed the opening/closing characters as the xml was not displaying correctly in my question):
<int-amqp:channel id="actionInstructionTransformed" message-driven="false"/>
<int-xml:unmarshalling-transformer
input-channel="actionInstructionXmlValid" output-channel="actionInstructionTransformed"
unmarshaller="actionInstructionMarshaller" />
I have got a poller defined with:
<int:poller id="customPoller" default="true" trigger="customPeriodicTrigger" task-executor="customTaskExecutor" max-messages-per-poll="${poller.maxMessagesPerPoll}" error-channel="drsGatewayPollerError" />
<int:transactional propagation="REQUIRED" read-only="true" transaction-manager="transactionManager" />
</int:poller>
In Java, I have got my consumer defined with:
#Transactional(propagation = Propagation.REQUIRED, readOnly = true, value = "transactionManager")
#ServiceActivator(inputChannel = "actionInstructionTransformed", poller = #Poller(value = "customPoller"),
adviceChain = "actionInstructionRetryAdvice")
public final void processInstruction(final ActionInstruction instruction)
From the documentation (http://docs.spring.io/autorepo/docs/spring-integration/4.0.2.RELEASE/reference/html/amqp.html), I understand that actionInstructionTransformed should be pollable as I have added message-driven="false".
When running my Spring Boot app, I am getting the exception: Caused by: java.lang.IllegalStateException: A '#Poller' should not be specified for Annotation-based endpoint, since 'actionInstructionTransformed' is a SubscribableChannel (not pollable).
I am using Spring Boot 1.4.4.RELEASE.
How can I force actionInstructionTransformed to be recognised as pollable?
Perhaps you are not importing the XML? In that case, the framework will create a DirectChannel for the service activator input channel.
This works fine for me...
#SpringBootApplication
#ImportResource("context.xml")
public class So42209741Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So42209741Application.class, args);
context.getBean("pollable", MessageChannel.class).send(new GenericMessage<>("foo"));
Thread.sleep(10000);
context.close();
}
#ServiceActivator(inputChannel = "pollable", poller = #Poller(fixedDelay = "5000"))
public void foo(String in) {
System.out.println(in);
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd">
<int-amqp:channel id="pollable" message-driven="false" />
</beans>

losing messages when calling http call with high concurrency

Hi I have stream definition like below.Where i pull file from s3 split line by line and call http client and put to named channel.My transport is rabbit and prefetch is 10 and concurrency of http is 100 and running on 3 container and 1 admin.
stream aws-s3|custom processor| custom-http-client --url1=https://test1.com --url2=https://test1.com --filterAttribute=messageAttribute --httpMethod=POST --nonRetryErrorCodes=400,401,404,500 --charset=UTF-8 --replyTimeout=30000 --mapHeaders=Api-Key,Content-Type --requestTimeOut=30000 |processor> queue:testQueue
my http-config looks like below and using apache http client for connection pooling and multithreaded I am putting back to DLQ all very errors like socket time out and retrying it .All not retry error 50x i am passing to next module and writing to error queue.But after I call my external rest API i am losing messages.I am sending around 220 k messages some time i get 200k messages some time i get all 220k and some time 210k its random.Not sure if i am doing anything wrong.I tried to increase the request time out socket time out.Till my processor before HTTP i get all message but after http client i see lesser messages in my named channel queue and nothing in error queue. But i am pretty sure messages are getting lost after calling http-client .This happens when there is high load of data like million and 200k+ records for lesser load like 500 to 1000 records i don't see this issue.
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- <context:property-placeholder location="${xd.module.config.location}\processor\${xd.module.name}\batch-http.properties"
ignore-resource-not-found="true" local-override="true"/> -->
<context:property-placeholder />
<!-- logger changes start -->
<channel-interceptor pattern="*" order="3">
<beans:bean class="org.springframework.integration.channel.interceptor.WireTap">
<beans:constructor-arg ref="loggingChannel" />
</beans:bean>
</channel-interceptor>
<logging-channel-adapter id="loggingChannel" log-full-message="true" level="ERROR"/>
<!-- logger changes end -->
<header-filter input-channel="input"
output-channel="inputX" header-names="x-death"/>
<service-activator input-channel="inputX" ref="gw" />
<gateway id="gw" default-request-channel="toHttp" default-reply-timeout="0" error-channel="errors" />
<beans:bean id="inputfields" class="test.HTTPInputProperties">
<beans:property name="nonRetryErrorCodes" value="${nonRetryErrorCodes}"/>
</beans:bean>
<beans:bean id="responseInterceptor" class="test.ResponseInterceptor">
<beans:property name="inputProperties" ref="inputfields" />
</beans:bean>
<chain input-channel="errors" output-channel="output">
<!-- examine payload.cause (http status code etc) and decide whether
to throw an exception or return the status code for sending to output -->
<header-filter header-names="replyChannel, errorChannel" />
<transformer ref="responseInterceptor" />
</chain>
<int-http:outbound-gateway id='batch-http' header-mapper="headerMapper"
request-channel='toHttp'
rest-template="batchRestTemplate"
url-expression="payload.contains('${filterAttribute}') ? '${url1}' : '${url2}'" http-method="${httpMethod}"
expected-response-type='java.lang.String' charset='${charset}'
reply-timeout='${replyTimeout}' reply-channel='output'>
</int-http:outbound-gateway>
<beans:bean id="batchHTTPConverter" class="org.springframework.http.converter.StringHttpMessageConverter" >
<beans:constructor-arg index="0" value="${charset}"/>
<beans:property name="supportedMediaTypes" value = "application/json;UTF-8" />
</beans:bean>
<beans:bean id="batchRestTemplate" class="testBatchRestTemplate" >
<beans:constructor-arg name="requestTimeOut" value="${requestTimeOut}"/>
<beans:constructor-arg name="maxConnectionPerRoute" value="${maxConnectionPerRoute}"/>
<beans:constructor-arg name="totalMaxConnections" ref="${totalMaxConnections}"/>
</beans:bean>
<beans:bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"
factory-method="outboundMapper">
<beans:property name="outboundHeaderNames" value="${mapHeaders}"/>
<beans:property name="userDefinedHeaderPrefix" value=""/>
</beans:bean>
<channel id="output" />
<channel id="input" />
<channel id="inputX" />
<channel id="toHttp" />
</beans:beans>
public class BatchRestTemplate extends RestTemplate{
private static final Logger LOGGER = LoggerFactory
.getLogger(BatchRestTemplate.class);
private static Integer requestTimeOut;
private static Integer totalMaxConnections;
private static Integer maxConnectionPerRoute;
public BatchRestTemplate(Integer requestTimeOut,Integer totalMaxConnections,Integer maxConnectionPerRoute) throws NoSuchAlgorithmException {
super(createBatchHttpRequestFactory());
List<HttpMessageConverter<?>> messageConverters= new ArrayList<HttpMessageConverter<?>>();
messageConverters.addAll(getMessageConverters());
messageConverters.add(0,new StringHttpMessageConverter(Charset.forName("UTF-8")));
setMessageConverters(messageConverters);
}
private static ClientHttpRequestFactory createBatchHttpRequestFactory() throws NoSuchAlgorithmException {
CloseableHttpClient httpClient;
HttpComponentsClientHttpRequestFactory httpRequestFactory;
SSLConnectionSocketFactory socketFactory;
socketFactory = new SSLConnectionSocketFactory(
SSLContext.getDefault(),
new String[] {"TLSv1"},
null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", socketFactory)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(250);
cm.setDefaultMaxPerRoute(100);
cm.closeExpiredConnections();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)
.setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm).build();
httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
return httpRequestFactory;
}
}
Response Interceptor
public class ResponseInterceptor {
private HTTPInputProperties inputProperties;
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseInterceptor.class);
/**
* Intercepts the errorMessage from the API response and sends appropriate
* information to the Output channel.
*
* #param errorMessage
* #return Message
*/
public Message<String> transform(Message<MessagingException> errorMessage) {
LOGGER.error("Inside Response Interceptor !");
Message<String> responseMessage = null;
try {
if (null != errorMessage && null != errorMessage.getPayload()
&& null != errorMessage.getPayload().getCause()) {
LOGGER.error("Cause is - " + errorMessage.getPayload().getCause().getMessage());
if (errorMessage.getPayload().getCause() instanceof HttpClientErrorException) {
HttpClientErrorException clientError = (HttpClientErrorException) errorMessage.getPayload()
.getCause();
LOGGER.error("Error in ResponseInceptor", clientError);
List<String> errorCodeList = getErrorCodes(inputProperties.getNonRetryErrorCodes());
// intercept Only those errors that are defined as
// nonRetryErrorCodes options in stream definition
if (null != clientError.getStatusCode()
&& errorCodeList.contains(clientError.getStatusCode().toString())) {
LOGGER.error("Error in Response Body", clientError.getResponseBodyAsString());
LOGGER.debug("Non retry message found. Sending to output channel without retrying");
responseMessage = MessageBuilder.withPayload((null == clientError.getResponseBodyAsString() || clientError.getResponseBodyAsString().isEmpty())
? getDefaultPayload(clientError.getStatusCode().toString()) : clientError.getResponseBodyAsString())
.setHeader(BatchHttpClientConstants.HTTP_STATUS, clientError.getStatusCode().toString())
.setHeader(BatchHttpClientConstants.REQUEST_OBJECT,
getFailedMessagePayload(errorMessage))
.copyHeaders(errorMessage.getPayload().getFailedMessage().getHeaders())
.setReplyChannelName(BatchHttpClientConstants.OUTPUT).setErrorChannelName(null).build();
} else {
LOGGER.debug("Status code from API is not present in the nonRetryCodes");
}
} else if (errorMessage.getPayload().getCause() instanceof HttpServerErrorException) {
LOGGER.error("Error is Instance of HttpServerErrorException");
HttpServerErrorException serverError = (HttpServerErrorException) errorMessage.getPayload()
.getCause();
responseMessage = MessageBuilder
.withPayload((null == serverError.getResponseBodyAsString()
|| serverError.getResponseBodyAsString().isEmpty())
? getDefaultPayload(serverError.getStatusCode().toString())
: serverError.getResponseBodyAsString())
.setHeader(BatchHttpClientConstants.HTTP_STATUS, serverError.getStatusCode().toString())
.setHeader(BatchHttpClientConstants.REQUEST_OBJECT, getFailedMessagePayload(errorMessage))
.copyHeaders(errorMessage.getPayload().getFailedMessage().getHeaders())
.setReplyChannelName(BatchHttpClientConstants.OUTPUT).setErrorChannelName(null).build();
}
}
} catch (Exception exception) {
LOGGER.error("Exception occured while transforming errorResponse", exception);
}
// returning null will send the message back to previous module
return responseMessage;
}
private String getDefaultPayload(String httpStatusCode) {
JSONObject jsonResponse = new JSONObject();
if (BatchHttpClientConstants.INTERNAL_SERVER_ERROR.equalsIgnoreCase(httpStatusCode)) {
jsonResponse.put(BatchHttpClientConstants.ID, BatchHttpClientConstants.INTERNAL_SERVER_ERROR_SUBCODE);
jsonResponse.put(BatchHttpClientConstants.TEXT, "Internal Server Error");
} else if (BatchHttpClientConstants.RESOURCE_NOT_FOUND.equalsIgnoreCase(httpStatusCode)) {
jsonResponse.put(BatchHttpClientConstants.ID, BatchHttpClientConstants.RESOURCE_NOT_FOUND_SUBCODE);
jsonResponse.put(BatchHttpClientConstants.TEXT, "Empty Response From the API");
}else{
jsonResponse.put(BatchHttpClientConstants.ID, BatchHttpClientConstants.GENERIC_ERROR_SUBCODE);
jsonResponse.put(BatchHttpClientConstants.TEXT, "Generic Error Occured.");
}
return jsonResponse.toString();
}
/**
* Get Individual error codes using delimiter
*
* #param nonRetryErrorCodes
* #return List of Error Codes as string
*/
private List<String> getErrorCodes(String nonRetryErrorCodes) {
List<String> errorCodeList = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(nonRetryErrorCodes, BatchHttpClientConstants.DELIMITER);
while (st.hasMoreElements()) {
errorCodeList.add(st.nextToken());
}
return errorCodeList;
}
/**
* returns failed Message Payload
*
* #param errorMessage
* #return String
* #throws UnsupportedEncodingException
*/
private byte[] getFailedMessagePayload(Message<MessagingException> errorMessage)
throws UnsupportedEncodingException {
if (null != errorMessage.getPayload().getFailedMessage()
&& null != errorMessage.getPayload().getFailedMessage().getPayload()) {
return errorMessage.getPayload().getFailedMessage().getPayload().toString()
.getBytes(BatchHttpClientConstants.UTF_8);
}
return "".getBytes(BatchHttpClientConstants.UTF_8);
}
public HTTPInputProperties getInputProperties() {
return inputProperties;
}
public void setInputProperties(HTTPInputProperties inputProperties) {
this.inputProperties = inputProperties;
}
}
I can recommend an <aggregator> as a diagnostic tool.
Send the message to the <int-http:outbound-gateway> (or even better in the beginning of your flow - on input channel).
And send that message to the <aggregator>, too.
Some key from the message should be used as a correlationKey.
Expect reply from HTTP Gateway as a second message in group to release.
The ReleaseStrategy is standard MessageCountReleaseStrategy based on the size = 2.
And here is the main trick of the <aggregator> - group-timeout, which should be a bit more than socket timeout. The "uncompleted" group (only request) should be discarded to some other channel, where you will be able to report those undelivered messages and consult with your REST Service what's going on with them.

Spring integration gateway "Dispatcher has no subscribers"

I am getting an exception Dispatcher has no subscribers on the outboundChannel and can't figure out why. I am sure its something simple, I have stripped back my code to a very simple sample below:
My context is:
<bean id="requestService"
class="com.sandpit.RequestService" />
<integration:channel id="inboundChannel" />
<integration:service-activator id="service"
input-channel="inboundChannel"
output-channel="outboundChannel"
ref="requestService"
method="handleRequest" />
<integration:channel id="outboundChannel" />
<integration:gateway id="gateway"
service-interface="com.sandpit.Gateway"
default-request-channel="inboundChannel"
default-reply-channel="outboundChannel" />
<bean class="com.sandpit.GatewayTester">
<property name="gateway"
ref="gateway" />
</bean>
My Java code is:
public interface Gateway {
String receive();
void send(String message);
}
public class RequestService {
public String handleRequest(String request) {
return "Request received: " + request;
}
}
public class GatewayTester implements ApplicationListener<ContextRefreshedEvent> {
private Gateway gateway;
public void setGateway(Gateway gateway) {
this.gateway = gateway;
}
#Override
public void onApplicationEvent(ContextRefreshedEvent event) {
gateway.send("Hello world!");
System.out.println("FROM SERVICE: " + gateway.receive());
}
}
Note: A breakpoint does tell me that the RequestService is actually handling the request.
receive() with no args needs the reply channel to be a PollableChannel See the documentation.
add <queue/> to the outboundChannel.
Alternatively, You could change your gateway method to be String sendAndReceive(String in) and all will work as expected (and you can even remove the outboundChannel altogether).

Resources