ScheduledTimerTask prevent overlap - java

I'm using spring's ScheduledTimerTask to schedule some tasks to schedule some tasks.
<bean id="someScheduler" class="com.example.scheduler.SomeScheduler">
</bean>
<bean id="anotherScheduler" class="com.example.scheduler.SomeScheduler">
</bean>
<bean id="someImport" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="someScheduler" />
</bean>
<bean id="anotherImport" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="anotherScheduler" />
</bean>
How to make sure anotherScheduler won't execute when someScheduler is being executed?

Related

How can I allow multiple #JmsListener destinations for a single method

I'm currently working on migrating an IBM Webshere application to Spring Boot.
As part of this there is an MDB class which needs to be converted into #JmsListener. This MDB has a single method that is listening to multiple queues. I would like to do the same using #JmsListener with multiple destinations. I saw this thread, but that's not working.
This is the current MDB Configuration :
Bean 1
<bean id="myAppabcResponseMDB" class="company.myApp.service.mdb.MyAppMessageListenerMDB"/>
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="destination" ref="myAppabcResponseDest"/>
<property name="messageListener" ref="myAppabcResponseMDB"/>
<property name="maxConcurrentConsumers" value="5"/>
<property name="sessionTransacted" value="true"/>
<property name="transactionManager" ref="transactionManager" />
<property name="taskExecutor" ref="myTaskExecutor" />
</bean>
<bean id="myAppabcResponseDest" name="jms/myAppESBResponse" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jms/myAppabcResponse"/>
<property name="resourceRef" value="true"/>
</bean>
<bean id="myAppRequestMDB" class="company.myApp.service.mdb.MyAppMessageListenerMDB"/>
<bean id="jmsContainer2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="destination" ref="myAppRequestDest"/>
<property name="messageListener" ref="myAppRequestMDB"/>
<property name="maxConcurrentConsumers" value="1"/>
<property name="sessionTransacted" value="true"/>
<property name="transactionManager" ref="transactionManager" />
<property name="taskExecutor" ref="myTaskExecutor" />
</bean>
<bean id="myAppRequestDest" name="jms/myAppRequest" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jms/myAppRequest"/>
<property name="resourceRef" value="true"/>
</bean>
#JmsListener is a repeatable annotation on Java 8, so you can associate several JMS destinations with the same method by adding additional #JmsListener declarations to it.
https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jms-annotated

Can mutiple quartz schedulers in a single application cause single log4j fileAppender stop rolling over?

Current application is on Spring 3.2/JDK7/Quartz/Log4J2, I have following quartz jobs scheduled below
Spring Quartz tiggers
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="trigger1" />
<ref bean="trigger2" />
<ref bean="trigger3" />
<ref bean="trigger4" />
<ref bean="trigger5" />
</list>
</property>
</bean>
Job interval definition
<bean id="trigger1" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="scheduledService1" />
<property name="startDelay" value="5000" />
<property name="repeatInterval" value="2900" />
</bean>
<bean id="trigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="scheduledService2" />
<property name="startDelay" value="7000" />
<property name="repeatInterval" value="3000" />
</bean>
<bean id="trigger3" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="scheduledService3" />
<property name="startDelay" value="9000" />
<property name="repeatInterval" value="3100" />
</bean>
<bean id="trigger4" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="scheduledService4" />
<property name="startDelay" value="11000" />
<property name="repeatInterval" value="3200" />
</bean>
<bean id="trigger5" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="scheduledService5" />
<property name="startDelay" value="13000" />
<property name="repeatInterval" value="2500" />
</bean>
I am trying to make them triggered in different time slot. In each scheduledService it referred to a service class with log4j logger instance. There is only one logger defined in log4j2.xml in the fileAppender, so all logging information from the application writing into the safe file.
My question is in such situation is it highly possible that if each logger write into the same logging file generated by Log4J2 then it may cause FileAppender stopped rolling (because of muti threads I/O in the same Log file, then the log file grows crazily with huge file size)? If so how to overcome this issue? Must create different log file for different service class? I wonder if Log4j2 somehow can make above scenario wor

How to clear cache using CacheConfiguration in Apache ignite

Not able to destroy the cache after some duration time using Spring CacheConfiguration in Apache Ignite.After 40 seconds cache should be cleared.
Please see the below Code.
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="myCache" />
<property name="atomicityMode" value="ATOMIC" />
<property name="backups" value="1" />
<property name="cacheMode" value="PARTITIONED"/>
<property name="expiryPolicyFactory"ref="createdExpiryPolicyForDay"/>
</bean>
<bean id="createdExpiryPolicyForDay" class="javax.cache.expiry.CreatedExpiryPolicy" factory-method="factoryOf" >
<constructor-arg type="javax.cache.expiry.Duration" ref="durationForDay"/>
<constructor-arg type="javax.cache.expiry.Duration" >
<util:constant static-field="javax.cache.expiry.Duration.FIVE_MINUTES"/>
</constructor-arg>
</bean>
<bean name="durationForDay" class="javax.cache.expiry.Duration" >
<constructor-arg name="timeUnit" value="SECONDS" />
<constructor-arg name="durationAmount" value="40"/>
</bean>
Please help us, we are stuck with this issue

OpenSAML / Spring security setup so redeploy on Tomcat works

We're running a webapp on OpenJDK IcedTea6 1.13.6 using Tomcat 6.0.35. For SSO we're using the Spring security SAML extension, which bases on OpenSAML 2.6.1.
When re-deploying our application (without restarting Tomcat), I get a
NoClassDefFoundError: org/bouncycastle/crypto/paddings/ISO10126d2Padding
which is very nicely described here. From an Eclipse MAT analysis, I gather that either BouncyCastleProvider or JCERSAPublicKey are preventing the WebappClassLoaderfrom being gc'ed.
How do I configure SAML so that all (bouncy castle) instances are properly destroyed? I have a hard time believing that SAML is designed such that a deployment mandates a Tomcat restart.
My current configuration:
...
<bean id="samlLogger" class="org.springframework.security.saml.log.SAMLDefaultLogger">
<property name="logErrors" value="true"/>
<property name="logMessages" value="true"/>
</bean>
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="classpath:security/samlKeystore.jks"/>
<constructor-arg type="java.lang.String" value="mypassword"/>
<constructor-arg>
<map>
<entry key="tenzingfaces" value="keyphrase"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="tenzingfaces"/>
</bean>
<bean id="samlEntryPoint" class="org.springframework.security.saml.SAMLEntryPoint">
<property name="defaultProfileOptions">
<bean class="org.springframework.security.saml.websso.WebSSOProfileOptions">
<property name="binding" value="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"/>
<property name="includeScoping" value="false"/>
</bean>
</property>
</bean>
<bean id="metadataDisplayFilter" class="org.springframework.security.saml.metadata.MetadataDisplayFilter"/>
<bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
<property name="userDetails" ref="samlUserDetailService" />
<property name="forcePrincipalAsString" value="false" />
</bean>
<bean id="samlUserDetailService" class="ch.umbrella.springframework.security.SamlUserDetailsServiceImpl" />
<bean id="contextProvider" class="org.springframework.security.saml.context.SAMLContextProviderImpl"/>
<bean id="samlSuccessRedirectHandler" class="ch.umbrella.springframework.security.SsoAuthenticationSuccessHandler" >
<property name="defaultTargetUrl" value="/main.html" />
<property name="alwaysUseDefaultTargetUrl" value="false" />
<property name="credentialsExpiredUrl" value="/credentialsexpired.html" />
</bean>
<bean id="samlWebSSOProcessingFilter" class="org.springframework.security.saml.SAMLProcessingFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationSuccessHandler" ref="samlSuccessRedirectHandler"/>
</bean>
<bean id="processor" class="org.springframework.security.saml.processor.SAMLProcessorImpl">
<constructor-arg>
<list>
<ref bean="redirectBinding"/>
<ref bean="postBinding"/>
</list>
</constructor-arg>
</bean>
<bean id="webSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerImpl"/>
<bean id="hokWebSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl"/>
<bean id="webSSOprofile" class="org.springframework.security.saml.websso.WebSSOProfileImpl"/>
<bean id="hokWebSSOProfile" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl"/>
<bean id="postBinding" class="org.springframework.security.saml.processor.HTTPPostBinding">
<constructor-arg ref="parserPool"/>
<constructor-arg ref="velocityEngine"/>
</bean>
<bean id="velocityEngine" class="org.springframework.security.saml.util.VelocityFactory" factory-method="getEngine"/>
<bean id="redirectBinding" class="org.springframework.security.saml.processor.HTTPRedirectDeflateBinding">
<constructor-arg ref="parserPool"/>
</bean>
<bean class="org.springframework.security.saml.SAMLBootstrap"/>
<bean id="parserPool" class="org.opensaml.xml.parse.StaticBasicParserPool" scope="singleton" init-method="initialize"/>
<bean id="parserPoolHolder" class="org.springframework.security.saml.parser.ParserPoolHolder" scope="singleton"/>
...
And, in a staging-specific file:
<bean id="metadata" class="org.springframework.security.saml.metadata.CachingMetadataManager">
<constructor-arg>
<list>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate" destroy-method="destroy">
<constructor-arg>
<bean class="org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider" destroy-method="destroy">
<constructor-arg ref="timer1" />
<constructor-arg>
<bean class="org.opensaml.util.resource.ClasspathResource">
<constructor-arg value="/security/idp.xml"/>
</bean>
</constructor-arg>
<property name="parserPool" ref="parserPool"/>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
</bean>
</constructor-arg>
</bean>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate" destroy-method="destroy">
<constructor-arg>
<bean class="org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider" destroy-method="destroy">
<constructor-arg ref="timer2" />
<constructor-arg>
<bean class="org.opensaml.util.resource.ClasspathResource">
<constructor-arg value="/security/localhost_sp.xml"/>
</bean>
</constructor-arg>
<property name="parserPool" ref="parserPool"/>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
<property name="local" value="true"/>
<property name="securityProfile" value="metaiop"/>
<property name="sslSecurityProfile" value="pkix"/>
<property name="signMetadata" value="true"/>
<property name="signingKey" value="tenzingfaces"/>
<property name="encryptionKey" value="tenzingfaces"/>
<property name="requireArtifactResolveSigned" value="false" />
<property name="requireLogoutRequestSigned" value="false" />
<property name="requireLogoutResponseSigned" value="false" />
<property name="idpDiscoveryEnabled" value="false" />
</bean>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<property name="hostedSPName" value="https://hurricane.umbrellanet.ch/uf-test/saml/metadata" />
</bean>
Thanks
Simon
If you are bundling the bouncycastle jars in your war, then don't do that. Instead put those jars in tomcat/lib . Hopefully, that would fix it.

How to execute method using spring beans

I need findItemByPIdEndDate() method of the MngtImpl class to be invoked every 5000ms, but nothing appears to be happening. Am I missing something?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl"/>
<property name="targetMethod" value="findItemByPIdEndDate"/>
<property name="repeatInterval" value="50000"/>
</bean>
#matt b I've read some of this, everything is new to me here ..so I came with this .. and again its not working, what am I missing this time ?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl" />
<property name="targetMethod" value="findItemByPIdEndDate" />
</bean>
<bean id="compareDateTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="findItemByPIdEndDate" />
<property name="startDelay" value="0" />
<property name="repeatInterval" value="50000" />
</bean>
For this task, the Chapter 23. Scheduling and Thread Pooling is your friend. That said, here is a short summary.
First, define your Job:
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl"/>
<property name="targetMethod" value="findItemByPIdEndDate"/>
</bean>
Now, you need to schedule the job using a trigger and a SchedulerFactoryBean. For the trigger, I suggest to use a SimpleTriggerBean in your case:
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="findItemByPIdEndDate" />
<!-- 10 seconds -->
<property name="startDelay" value="10000" />
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="50000" />
</bean>
To finalize everything, set up the SchedulerFactoryBean:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
You need a lot more plumbing than that to make Quartz work. Just declaring the MethodInvokingJobDetailFactoryBean on its own will do nothing.
However, Quartz is overkill for this, Java5+ can do this on its own. I suggest reading up on Spring's ScheduledExecutorFactoryBean, which in combination with MethodInvokingRunnable, allows you to invoke your method periodically.
What you've done so far is the equivalent of only instantiating a MethodInvokingJobDetailFactoryBean() - essentially all you've done is created the Job. Now you need to have some configuration for how it's scheduled, and what triggers it.
Take a look at the section in the Spring manual on Quartz.

Categories

Resources