Spring Integration - Invoking Methods in Application Code - java

I have a outbound-channel-adapter, where the relevant configuration is shown below.
<int:outbound-channel-adapter channel="foo-fileChannel" ref="foo-handlerTarget" method="handleFeedFile">
<int:poller fixed-delay="5000" receive-timeout="1000" max-messages-per-poll="10" />
</int:outbound-channel-adapter>
<int:channel id="foo-fileChannel">
<int:queue />
</int:channel>
<bean id="foo-handlerTarget" class="com.abc.FooFeedHandlerImpl">
<property name="fooDescriptorFile" value="${feed.foo.fooDescriptorFile}" />
<property name="fileIdRegex" ref="foo-fileRegex" />
<property name="processId" value="${feed.processId}" />
<property name="workingLocation" value="${feed.foo.workingLocation}" />
<property name="remoteLocation" value="${feed.foo.remoteLocation}" />
<property name="stalenessThreshold" value="${feed.foo.stalenessThreshold}" />
</bean>
And in FooFeedHandlerImpl...
public void handleFeedFile(File retrievedFile) {
handleFeedFile(retrievedFile, null);
}
public void handleFeedFile(File retrievedFile, String processKey) {
if (isHandlerForFileName(retrievedFile.getName())) {
processFeed(retrievedFile, processKey);
}
}
Questions:
Which handleFeedFile method gets invoked by the channel adapter?
When I invoke a method in the application code using Spring integration, how are the method parameters determined?
Thanks for any help!
Edit:
I ran my process locally (downloaded a local SFTP server - http://www.coreftp.com/server/index.html) and determined that the handleFeedFile(File file) method was invoked.

You probably want to refer to F.6 Message Mapping rules and conventions.
Multiple parameters could create a lot of ambiguity with regards to determining the appropriate mappings. The general advice is to annotate your method parameters with #Payload and/or #Header/#Headers Below are some of the examples of ambiguous conditions which result in an Exception being raised.
and:
Multiple methods:
Message Handlers with multiple methods are mapped based on the same rules that are described above, however some scenarios might still look confusing.
If you're not in a position to annotate your target methods, then you might be able to use a SpEL expression to call your intended method:
3.3.2 Configuring An Outbound Channel Adapter
Like many other Spring Integration components, the and also provide support for SpEL expression evaluation. To use SpEL, provide the expression string via the 'expression' attribute instead of providing the 'ref' and 'method' attributes that are used for method-invocation on a bean. When an Expression is evaluated, it follows the same contract as method-invocation where: the expression for an will generate a message anytime the evaluation result is a non-null value, while the expression for an must be the equivalent of a void returning method invocation.

According to the documentation on Spring integration, the POJO (bean foo-handlerTarget) in your case will get called with a Message object containing the payload. Have you executed your code? I'd expect it generates a NoSuchMethodError.
You need a
public void handleFeedFile(Message<?> message);
method.

Related

Set a spring integration router attribute programmatically

We have a Spring Integration route using a Router with an expression. This expression is set in the yml file.
I would like to create unit tests setting manually the value for this router attribute, but it doesn't seem to work.
Test
#Autowired
EventDrivenConsumer myrouter;
.....
((ExpressionEvaluatingRouter) myrouter.getHandler()).setPrimaryExpression(new SpelExpressionParser().doParseExpression("true"));
Part of the XML context
<int:router input-channel="catchweightExcluderChannel" expression="${rip.config.exclude_catchweight}" default-output-channel="productTransformerChannel">
<int:mapping value="true" channel="catchWeightFilteringChannel" />
<int:mapping value="false" channel="productTransformerChannel" />
</int:router>
But it seems my value overrided is not affecting the behaviour...it seems as once Spring reads the context, changing attributes of components does not affect their behaviour.
You can't change the property that way because expression is a ctor arg for the ExpressionEvaluatingRouter:
public class ExpressionEvaluatingRouter extends AbstractMessageProcessingRouter {
public ExpressionEvaluatingRouter(Expression expression) {
super(new ExpressionEvaluatingMessageProcessor<Object>(expression));
setPrimaryExpression(expression);
}
}
I think for your test use-case the #TestPropertySource would be the best solution:
#RunWith(SpringRunner.class)
#TestPropertySource(properties = "rip.config.exclude_catchweight:true")
UPDATE
but then why is there a mutator called setPrimaryExpression
The setPrimaryExpression() and its sibling getExpression() for visualization tools and logs to make expression-based components much cleaner for analysis. Since one components may have several expressions, we decided to distinguish a prime one and therefore that name for a setter.
The real hard work for expression evaluation is done there in the ExpressionEvaluatingMessageProcessor, which is ctor-based as well, as we see.
For your per method requirements I only can suggests to create ApplicationContext manually and call its setEnvironment(new MockEnvironment(). And already there you can populate a desired value for that expression in particular case.
You can't override that ctor-based initialization when your application context is already started.

Can Spring 3 formBackingObject return different command classes?

I have a situation where I need to check a condition in my formBackingObject and, depending on the condition, return one of two classes.
The problem is that, so far as I know, I can only define one commandName and commandClass in the servlet.xml. Anyone know of a way I can handle this? It doesn't seem like a rare use case, but I haven't really found any solution on the net.
Here is the logic block from my controller formBackingObject:
List<FooLoadShed> fooLoadShedList = this.fooLoadShedDao.getActiveSheds();
if(fooLoadShedList.isEmpty()) {
logger.info("LoadShedActive is: " + this.sessionDetailsManager.getSessionDetails().isLoadShedActive());
return new NoAction();
}
else {
this.sessionDetailsManager.getSessionDetails().setLoadShedActive(true);
logger.info("LoadShedActive is : " + this.sessionDetailsManager.getSessionDetails().isLoadShedActive());
logger.info("Number of load sheds: " + nieLoadShedList.size());
return new ModelAndView(new RedirectView("custLookup.htm"));
}
and my servlet.xml config:
<bean name="/index.htm" class="springapp.web.indexController" scope="session">
<property name="sessionForm" value="true"/>
<property name="commandName" value="noAction"/>
<property name="commandClass" value="springapp.service.NoAction"/>
<property name="formView" value="index"/>
<property name="sessionDetailsManager" ref="sessionDetailsManager"/>
<property name="mobiConfigDao" ref="mobiConfigDao"/>
<property name="fooLoadShedDao" ref="fooLoadShed" />
</bean>
This is a very old way to configure controllers Spring MVC! Haven't seen something like this in over 10 years. Why not use more modern Spring MVC configuration, using annotations instead of XML?
In any case, the importance of the command class is in the POST. Spring MVC must be able to construct an instance of the command class. In order to do that, it needs to know the specific class name. Then it will apply the form values to the properties on the command class object that was created. This fully populated command object will be handed to you in the handler method.
The configuration in the XML is for the default command object creation process. You can override this. In modern Spring MVC, this is with a method level #ModelAttribute annotation. In ancient Spring MVC, you need to override the methods that create the command object and create the command object yourself. BaseCommandController has a createCommand method which is protected. If that's the way you created your controller, that's where you would have to start.

#Cacheable not working

I am using #Cacheable for caching the result of a method at Service layer in Spring 3.2. following method code is used inside service Class:
#Cacheable("questions")
public List<ABClassObject> getSecutityQuestionsList(){
List<ABClassObject> list = new ArrayList<ABClassObject>();
----------------
list = ----[DAO call]
return list;
}
xml Configuration
<cache:annotation-driven />
<!-- Generic cache manager based on the JDK ConcurrentMap -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="questions" />
</set>
</property>
</bean>
Can't use EhCache because of using jdk 1.6.
By using the above code pattern i am unable to cache the List result.DAO is called all the time when i call the above method.
So, Suggest me whats wrong with the code.
Thanks in advance.
Some things you should check:
The class of getSecutityQuestionsList method is a spring bean, i.e, you donĀ“t use a new operator anyway.
The method getSecutityQuestionsList is called from another bean
In your xml configuration put a context:component-scan base-package="xxxxx"
Put a break point inside your method. In the stack trace you should see some spring proxy stuff. When you call this method of your service, you should actually be calling a spring proxy.

Spring putting dynamically generated values into placeholders

I am new to Spring. I now understand how to use placeholders to read values from a properties file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties.txt"/>
</bean>
<int-mqtt:outbound-channel-adapter id="mqtt-publish"
client-id="${clientID}"
client-factory="clientFactory"
auto-startup="true"
url="${url}"
default-qos="${qos}"
default-retained="${retain}"
default-topic="${topic}" />
Everything works fine with the code above... But... Is it possible for instance to replace the clientID by something generated at runtime (or from user input) instead of statically reading it from a properties file?
By runtime, do you mean dynamically for each message?
In that case, no, because the clientId is used while establishing the connection, which is done once (or when the connection to the server is lost).
If you mean to provide a dynamic value programmatically when the application context initializes, then, yes, the Spring Expression Language is the solution.
For example, #{myBean.myProperty} will call the getMyProperty() method on a bean myBean and #{myBean.someMethod()} will invoke someMethod().
Also see the dynamic-ftp sample, which uses placeholders at runtime by creating a new outbound adapter on demand using property placeholders, in a child application context.

Spring Config file consisting of List of Implementations

I Am very new to Spring. I have an Interface (MessageHandler ) which has a get method, this method returns a list of Implementations of another interface (messageChecker).
public interface MessageHandler {
public void process(BufferedReader br);
public void setMessageCheckerList(List mcList);
[B]public List getMessageCheckerList();[/B]
}
In my Spring XML configuration , i have something like this ,along with other beans
<bean id="messageHandler" class="com.XXX.messagereceiver.MessageHandlerImpl">
<property name="messageCheckerList" ref="checkerList"/>
</bean>
<bean id="checkerList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="HL7Checker"/>
</list>
</constructor-arg>
</bean>
<bean id="HL7Checker" class="com.XXX.messagereceiver.HL7CheckerImpl">
<property name="messageExecutor" ref="kahootzExecutor"/>
</bean>
Here i am passing a checkerlist - which is a list of Implementations ( For now i have only 1) of the Interface (messageChecker)
Checkerlist is containing references to Bean Id's which are actual implementaions.
HL7Checker is an implementation of an Interface messageChecker.
But when i run the main program, When i inject the bean "messageHandler" and call the getMessageCheckerList, It returns a null value. These getter and setter methods are working fine without using spring.
I am not sure what seems to be the problem.
I don't know the answer for you troubles, but I would check:
is the setter setMessageCheckerList(List) in messageHandler bean called? (either using some debugger or some trace output like System.out...). If it's not, there's probably something wrong with your Spring XML configuration setup. The bean definition you posted requires the property to be set and Spring wouldn't create the messageHandler bean without setting the property.
who calls the setMessageCheckerList(List) setter? Or even more precise, what code writes to the field which stores the value of the property? Maybe the field is initialized properly by Spring but gets overwritten to null later on?
are you sure you call the getMessageCheckerList on the very same object Spring has configured for you (that is, the messageHandler bean). The definition you have posted clearly states an instance of MessageHandlerImpl is created by Spring, but it doesn't prevent other instances to be created in other ways. So maybe the instance created by Spring holds the proper value, but you run the get... on a wrong instance?

Categories