How to customize the exception handling for #Scheduled annotation from spring ?
I have Cron jobs which will be triggered in the server (Tomcat 6) and when any exceptions occur I need to do some handling.
Spring version 3.2
Tomcat Server 6
If you want to use Java Config you will need to create configuration implementing SchedulingConfigurer
#EnableScheduling
#Configuration
class SchedulingConfiguration implements SchedulingConfigurer {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final ThreadPoolTaskScheduler taskScheduler;
SchedulingConfiguration() {
taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setErrorHandler(t -> logger.error("Exception in #Scheduled task. ", t));
taskScheduler.setThreadNamePrefix("#scheduled-");
taskScheduler.initialize();
}
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler);
}
}
You can modify error handler for your needs. Here I only log a message.
Don't forget to call taskScheduler.initialize();. Without it you'll get:
java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized
You could implement and register an ErrorHandler for the ThreadPoolTaskScheduler that is used for your scheduling annotations.
<task:annotation-driven scheduler="yourThreadPoolTaskScheduler" />
<bean id="yourThreadPoolTaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="5" />
<property name="errorHandler" ref="yourScheduledTaskErrorHandler" />
</bean>
<bean id="yourScheduledTaskErrorHandler"
class="com.example.YourScheduledTaskErrorHandler"/>
Why not wrap your business logic and do a simple try catch in your #schedule method. Then you can log or take whatever action is necessary for failure cases.
#Scheduled(cron = "${schedulerRate}")
public void scheduledJob() {
try {
businessLogicService.doBusinessLogic();
} catch (Exception e) {
log.error(e);
}
}
Related
In this example, I am trying to set the object dependency before calling businessLogic. I am receiving a nullpointer because that 'consumer' object is not set.
Here is the basis of the example and mostly trying to use the Spring DSL.
http://camel.apache.org/polling-consumer
Section: Timer based polling consumer
Here is my camel/spring config:
<bean id="simpleOutboxMessageConsumer" class="org.berlin.camel.esb.logs.mq.SimplePrintMessageConsumer"/>
<!-- Continue with spring dsl for ESB -->
<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">
<!-- Define a MQ consumer template -->
<consumerTemplate id="consumer" />
....
</camelContext>
<route id="fromOutboxAndConsume">
<from uri="timer://foo?period=30000" />
<to uri="bean:simpleOutboxMessageConsumer?method=businessLogic" />
</route>
Java code
#Component
public class SimplePrintMessageConsumer {
private static final Logger logger = Logger.getLogger(SimplePrintMessageConsumer.class);
private int count;
#Autowired
private ConsumerTemplate consumer;
public void setConsumer(final ConsumerTemplate consumer) {
this.consumer = consumer;
}
public void businessLogic() {
logger.info("Launching business logic to consume outbox, blocking until we get a message >>>");
while (true) {
// Consume the message
final String msg = consumer.receiveBody("activemq:queue.outbox", 3000, String.class);
logger.info("Printing message found from queue: " + msg);
if (msg == null) {
// no more messages in queue
break;
}
}
}
}
There is a nullpointer at the usage of the consume object. I am thinking that spring is not just autowiring that bean properly. Even if I didn't use spring, how would I pass the consumer template object to this bean?
This should work
<bean id="simpleOutboxMessageConsumer" class="....SimplePrintMessageConsumer">
<property name="consumer" ref="consumer"/>
</bean>
Remove the #AutoWire , I am checking on why the #Autowire is not working by the way
Last year, spring integration released 4.0 version for us to configure using annotation without configuring in XML files. But I want to use this feature using the existing XML configurations.
So I wrote the code using spring boot and integration annotation
#Configuration
#ComponentScan(basePackages ={"com.strongjoe.blue"},excludeFilters=#ComponentScan.Filter(type=FilterType.REGEX, pattern={"com.strongjoe.blue.agent.Bootstrap*"}))
#IntegrationComponentScan
#ImportResource( {"${context.agent.path}context-bean-*.xml", // Context Configuration
"${project.agent.path}context-properties.xml" } ) // Project Based Chain configuration
public class AgentStarter implements CommandLineRunner{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
#Lazy
#Bean
#ServiceActivator(inputChannel="blue-hub-start-channel", outputChannel="blue-hub-route-channel")
public <T> BlueMessage<T> startJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent started :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
#Lazy
#Bean
#ServiceActivator(inputChannel="blue-hub-end-channel")
public <T> BlueMessage<T> endJob(BlueMessage<T> msg) {
logger.debug("BluE Hub Agent ended :{} [phrase:{}]", System.currentTimeMillis() , "prototype-version");
return msg;
}
#Bean
#Transformer(inputChannel="blue-normalized-channeel", outputChannel="blue-output-channel")
public org.springframework.integration.transformer.Transformer JsonToMap( ) {
return new JsonToObjectTransformer( List.class );
}
#MessageEndpoint
public static class Echo {
#ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
#Autowired
Gateway gateway;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(AgentStarter.class);
app.setWebEnvironment(false);
app.run(args).close();
}
#Override
public void run(String... args) throws Exception {
System.err.println("blue-hub-agent started..");
System.out.println(gateway.sendReceive("gitlab"));
}
And I wrote the definition about every channel I use in the 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-ws="http://www.springframework.org/schema/integration/ws"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
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-4.0.xsd
http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-4.0.xsd">
<int:channel id="blue-normalized-channel" />
<int:channel id="blue-header-channeel" />
<int:channel id="blue-request-channel" />
<int:channel id="blue-output-channel" />
<int:channel id="blue-gitlab-request-prepare-channel" />
<int:channel id="blue-hub-start-command-channel" />
<int:channel id="blue-hub-start-channel"/>
<int:channel id="blue-hub-end-channel" />
But I got error.
Caused by: org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:8090.blue-hub-start-channel'.
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:81)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:223)
The reason will be, I think,
that spring bean in XML file and spring bean with the annotation has different context. So I think that even if blue-hub-start-channel is subscribed by the service activator named "startJob", it got error.
How can I solve this problem?
Annotating #ServiceActivator on #Bean is not for POJO Messaging. See the documentation.
When you annotate #Beans this way, you have to provide an appropriate object (MessageHandler in this case).
For POJO style annotated methods, the annotation must be on a method in a #Bean method (like you have on this one...
#MessageEndpoint
public static class Echo {
#ServiceActivator(inputChannel="blue-output-channel")
public void stringEcho(Message message) {
}
}
... and then declare a #Bean for Echo.
You can put all your #ServiceActivator methods (and #Transformers) in the same #MessageEndpoint.
I am unit-testing with Spring Framework's EmbeddedDatabaseBuilder as a datasource and passing this into the Hibernate config for my SessionFactory, using Spring 4 and Hibernate 4. I am not using the Spring context in any way - the only config I use is programmatic (no annotations, no XML except the Hibernate mapping files).
I expected that Hibernate would use its default ThreadLocalSessionContext and that I would be able to start and rollback transactions in the unit test.
However somehow Hibernate has set its SessionFactory.currentSessionContext to Spring's SpringSessionContext and this complains whenever I try to call SessionFactory.getCurrentSession():
HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
In the code below in my unit test, I have set hibernate.current_session_context_class to thread but this is ignored or replaced with the Spring implementation.
public abstract class HibernateTestBase {
private static EmbeddedDatabase dataSource;
private static SessionFactory sessionFactory;
private Session session;
#BeforeClass
public static void setupClass() {
dataSource = new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("file:SQLResources/schema-1.1.sql").
addScript("file:SQLResources/schema-1.2.sql").
build();
Configuration configuration = new Configuration();
configuration.addResource("hibernate-mappings/HierarchyLevel.hbm.xml");
configuration.addResource("hibernate-mappings/HierarchyFilter.hbm.xml");
configuration.addResource("hibernate-mappings/AuditLog.hbm.xml");
configuration.setProperty("hibernate.dialect",
"org.hibernate.dialect.Oracle10gDialect");
configuration.setProperty("hibernate.show_sql", "true");
configuration.setProperty("hibernate.current_session_context_class",
"thread");
StandardServiceRegistryBuilder serviceRegistryBuilder =
new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySetting(Environment.DATASOURCE, dataSource);
serviceRegistryBuilder.applySettings(configuration.getProperties());
StandardServiceRegistry serviceRegistry =
serviceRegistryBuilder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
sessionFactory.openSession();
}
#AfterClass
public static void tearDown() {
if (sessionFactory != null) {
sessionFactory.close();
}
if (dataSource != null) {
dataSource.shutdown();
}
}
#Before
public final void startTransaction() {
session = sessionFactory.getCurrentSession();
session.beginTransaction();
}
#After
public final void rollBack() {
session.flush();
Transaction transaction = session.getTransaction();
transaction.rollback();
}
}
I am forced to call SessionFactory.openSession() instead but then I won't be able to use my DAOs with the same SessionFactory because they all call getCurrentSession(). So I won't be able to benefit from the already coded data handling functionality.
What can I do?
It works OK when setting up the Configuration object using a traditional hibernate.cfg.xml using the SpringSessionContext as for production like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<!-- property name="hibernate.current_session_context_class">thread</property-->
<mapping resource="hibernate-mappings/AccessRight.hbm.xml" />
</session-factory>
</hibernate-configuration>
and loading that up in the test, but overwriting the session context like this:
Configuration configuration = new Configuration();
configuration.configure(
"hibernate-mappings/hibernate.cfg.xml");
configuration.setProperty("hibernate.current_session_context_class",
"thread");
So there is something going on in the background when Hibernate sets itself up with its hibernate.cfg.xml that is required.
I have followed the spring documentation and setup a Spring JMS listener. Yet, even if I add a message to the queue, my code is not detecting this. My spring config is as follows:
<bean id="dataSourceListener" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="xxx"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
</bean>
<bean id="jmsConnectionFactory" class="OracleAqFactoryBean">
<property name="dataSource" ref="dataSourceListener" />
</bean>
<jms:listener-container connection-factory="jmsConnectionFactory" acknowledge="transacted" concurrency="1-5">
<jms:listener destination="queuename" ref="myMessageListener"/>
</jms:listener-container>
<bean id="myMessageListener" class="Listener"/>
My Java is as follows:
My custom listener:
class Listener implements MessageListener {
#Override
void onMessage(Message message) {
// code to handle message is here
}
}
And my OracleAqFactoryBean:
public class OracleAqFactoryBean implements FactoryBean {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
#Override
public Object getObject() throws Exception {
return AQjmsFactory.getConnectionFactory( dataSource );
}
#Override
public Class<?> getObjectType() {
return ConnectionFactory.class;
}
#Override
public boolean isSingleton() {
return true;
}
}
[EDIT: THE ABOVE SETUP IS NOW WORKING SUCCESSFULLY]
I do not understand why you are wiring up a FactoryBean implementation to the Spring DMLC destination property. This is clearly not correct because the setDestinationmethod only accepts a javax.jms.Destination type. You've wired up the connectionFactory and the messageListener. That's all that's needed to begin consuming messages. If you remove the testmq ref that you have wired to the destination property, then messages should be successfully consumed.
I have log4j DailyRollingFileAppender class in which setFile() method I need to check database value to decide which file to used for logging.
DailyRollingFileAppender class
public void setFileName()
{
isLoginEnabled = authenticationManager.checkLoginLogging();
}
Here 'authenticationManager' is object of class used to make database call using spring dependency injection feature.
spring-beans.xml
<bean id="dailyRollingFileAppender" class="com.common.util.DailyRollingFileAppender">
<property name="authenticationManager">
<ref bean="authenticationManager"/>
</property>
</bean>
<bean id="authenticationManager" class="com.security.impl.AuthenticationManagerImpl">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>
Now when I start my application log4j gets initiated first and since spring-beans is yet to invoked it throws NullPointerException in method setFileName().
So is there a way I can make call to 'authenticationManager.checkLoginLogging();' from DailyFileAppender class so that when log4j loads it should able to get database value?
A few years late, but I hope this is of help to someone.
I was after similar functionality - I have a custom appender, and i wanted to use an autowired bean to perform some logging using a service we'd built. By making the appender implement the ApplicationContextAware interface, and making the field that i'd normally autowire static, i'm able to inject the spring-controlled bean into the instance of the appender that log4j has instantiated.
#Component
public class SslErrorSecurityAppender extends AppenderSkeleton implements ApplicationContextAware {
private static SecurityLogger securityLogger;
#Override
protected void append(LoggingEvent event) {
securityLogger.log(new SslExceptionSecurityEvent(SecurityEventType.AUTHENTICATION_FAILED, event.getThrowableInformation().getThrowable(), "Unexpected SSL error"));
}
#Override
public boolean requiresLayout() {
return false;
}
#Override
public synchronized void close() {
this.closed = true;
}
#Override
public void setApplicationContext(ApplicationContext applicationContext) {
if (applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger") != null) {
securityLogger = (SecurityLogger) applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger");
}
}
}