Spring task scheduler delay from jndi - spring

I'm configuring a Spring task scheduler.
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="servicesConfigurationBean" method="loadResources" fixed-delay="300000" />
</task:scheduled-tasks>
It's working properly. Now I would like to set the delay value from a JNDI lookup. So I tried the following:
<task:scheduled ref="servicesConfigurationBean" method="loadResources">
<property name="fixed-delay"><ref local="servicesRefreshRate"/></property>
</task:scheduled>
But now I obtain the following exception:
[/WEB-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'task:scheduled' must have no character or element information item [children], because the type's content type is empty.[/WEB-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'task:scheduled' must have no character or element information item [children], because the type's content type is empty.
I understand the cause of the exception, so, is there a viable solution to my problem?
Thanks.

Using <property> won't work, because <task:scheduled> is a macro, not a bean definition. The two work very differently.
Try using Spring-EL instead:
<task:scheduled ref="servicesConfigurationBean"
method="loadResources"
fixed-delay="#servicesRefreshRate" />
I haven't tried it, but give it a go.

Related

Spring EL template in a spring bean definition

I have a bean that has a map injected. The entries in the map are of type myBean (see below) and are defined in my spring XML. It may be that not all properties of myBean are defined in the xml, so I thought I would provide defaults in the form of a template where the variable bit in the template is provided by another property that will be present (p1 below). I hope that makes sense.
I am sure there are other ways to achieve this; well, I know there are, but I am new to Spring and I came across Spring EL and it sounded like it was suited to this kind of thing, so I tried this:
<bean id="myDefaults" class="com.myco.MyDefaults">
<property name="prop1" value="abc#{this.p1}def"/>
</bean>
<bean id="myBean" class="com.myco.MyBean" abstract="true">
<property name="theDefaults" ref="myDefaults"/>
</bean>
In MyBean the value of p1 is 100, and I was hoping that the value of theDefaults would be abc100def, but instead I get an error:
Error creating bean with name 'myDefaults' defined in class path resource [myapp-spring.xml]:
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'this' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Thanks for any help
Paul
The variable is #this, not this but in any case, it's not needed in this context, it's implied.
Use value="abc#{p1}def"

Mule invoke message processor

I came across "invoke" element (link) that can be used to invoke java methods from inside the flow. It seemed a perfect solutions for me, since I don't want to use Callable and entry points resolvers and I want to pass extra parameter to the method.
<invoke object-ref="yourBean"
method="yourMethod"
methodArguments="#[message.inboundProperties['inboundPropertyName']]" />
<set-property propertyName="outboundPropertyName"
value="#[payload]" />
I did something like the code above. My question is: how should I create "yourBean"?
I tried to create:
<spring:bean name="yourBean" class="class"/>
but got:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'yourBean' is defined
I also have other beans defined in my tag, but others seemed to work. Does anyone have similar issue?
EDIT:
When I deleted other beans definitions besides "yourBean" everything started to work, but when I add more beans definitions I got following error when I tried to use "yourBean":
Exception stack is:
1. null (java.lang.NullPointerException)
org.mule.processor.InvokerMessageProcessor:280 (null)
2. null (java.lang.NullPointerException). Message payload is of type: String (org.mule.api.MessagingException)
org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:35 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.ht
ml)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.NullPointerException
at org.mule.processor.InvokerMessageProcessor.transformArgument(InvokerMessageProcessor.java:280)
at org.mule.processor.InvokerMessageProcessor.evaluateArguments(InvokerMessageProcessor.java:200)
at org.mule.processor.InvokerMessageProcessor.process(InvokerMessageProcessor.java:164)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
Which for me seems like the "yourBean" is not instantiated. But why?
My bean definitions:
<bean id="yourBean" class="com.example.BpmService"/>
<bean id="StatusUpdateContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg>
<array>
<value type="java.lang.Class">
com.example.OrderStatusUpdate
</value>
</array>
</constructor-arg>
</bean>
EDIT: Ok, id/name both some to work for Mule invoke. I have had no problems using invoke with multiple bean definitions. This sounds more like an issue with your beans, which you are not sharing, and/or Spring than Mule invoke.

Is it possible to populate spring util:list via properties file?

I would like to populate transport End points via properties file. I tried this but it didn't work
<util:properties id="cxfProperties" location="/WEB-INF/classes/cxf.properties" />
<util:list id="transportEndpoints">
<!--
<value>http://localhost:8080/doubleit/services/doubleit.*</value>
-->
<value>#{cxfProperties.service.wsdllocation}</value>
</util:list>
In my properties file I have
service.wsdllocation=http://localhost:8080/doubleit/services/doubleit.*
I get error:
Expression parsing failed; nested exception is
org.springframework.expression.spel.SpelEvaluationException:
EL1008E:(pos 14): Field or property 'service' cannot be found on
object of type 'java.util.Properties'
I don't think SpEL provides direct field access syntax for property in Properties. So I think the correct syntax should be:
#{cxfProperties.getProperty('service.wsdllocation')}
or
#{cxfProperties.getProperty('service.wsdllocation', 'SOME_DEFAULT_VAL')}

Spring batch/integration bean initialization error for remote directory in outbound adapter

I am trying to use spring batch/integration in the following way:
<bean id="ftpRemoteDirectory" class="java.lang.String" scope="step">
<constructor-arg type="java.lang.String" value="#{jobExecutionContext.get ('batchProperties').getProperty('remote.directory')}"/>
</bean>
<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
channel="sftpChannel"
session-factory="sftpSessionFactory"
remote-directory-expression="#{ ftpRemoteDirectory }"
charset="UTF-8"
use-temporary-file-name="false"/>
However I receive the following error:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#13': Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: Could not convert constructor argument value of type [com.sun.proxy.$Proxy12] to required type [java.lang.String]: Failed to convert value of type 'com.sun.proxy.$Proxy12 implementing java.io.Serializable,java.lang.Comparable,java.lang.CharSequence,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy12 implementing java.io.Serializable,java.lang.Comparable,java.lang.CharSequence,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [java.lang.String]: no matching editors or conversion strategy found
The goal is to have remote-directory populated from the arguments passed into the JVM from spring batch admin. That is why scope="step" is present on the ftpRemoteDirectory bean - so that we have access to the jobExecutionContext object. So, i'm trying to get the property from the jobExecutionContext and then inject it into the remote-directory attribute of the outbound channel adapter.
I'd be open to suggestions for other ways to do this as well - I just can't get it to work. Thanks!

spEL in Bean Definition File : How to get a value in an inline list?

Given an inline list from a properties file:
nameList=john,smith,junior
From the bean definition file, I want to always extract the first item in my list. In this case, I would extract 'john'.
Using Spring Framework 3.0.4.RELEASE, my bean property looks as follows:
<property name="mySingleName" value="${nameList}" />
This obviously gives me the whole list.
I attempted to use the following to get the first item in the name list, but was met with an error:
<property name="mySingleName" value="#{${nameList}[0]}" />
The error was:
BeanExpressionException:Exception parsing failed: After parsing a valid expression, there is still more data in the expression:'comma(,)'
I have looked in the Book "Spring In Action", and have looked at the Spring spEL Documentation on Springs website. I have also googled for a quite a bit. Unfortunately, most of the code examples are done in code and not on the spring definition files. Any assistance in this would be greatly appreciated.
In your example you are reading a "nameList" property as a String ( not as a java.util.List ). You would need to convert it to a list first:
<bean id="listOfNames" class="org.springframework.util.StringUtils" factory-method="commaDelimitedListToSet">
<constructor-arg type="java.lang.String" value="${nameList}"/>
</bean>
and then you can apply SPeL to it:
<property name="mySingleName" value="#{listOfNames[0]}" />
similar problem / solution here: Spring: Reading collections form property files

Resources