OPEN - issue BATCH-378: RepeatListener is confusing and too generic to use for 'intercepting' a step

http://jira.springframework.org/browse/BATCH-378

Remove unnecessary listener implementations and migrate some functionality to samples.  In the process discovered that tasklet steps cannot be stopped.
This commit is contained in:
dsyer
2008-02-28 11:41:49 +00:00
parent 707e94b3c8
commit 2a25a7a636
16 changed files with 183 additions and 358 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.advice;
import javax.management.Notification;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;
/**
* JMX notification broadcaster
*
* @author Dave Syer
* @since 2.1
*/
public class JobExecutionNotificationPublisher implements ApplicationListener, NotificationPublisherAware {
protected static final Log logger = LogFactory.getLog(JobExecutionNotificationPublisher.class);
private NotificationPublisher notificationPublisher;
private int notificationCount = 0;
/**
* Injection setter.
*
* @see org.springframework.jmx.export.notification.NotificationPublisherAware#setNotificationPublisher(org.springframework.jmx.export.notification.NotificationPublisher)
*/
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
this.notificationPublisher = notificationPublisher;
}
/**
* If the event is a {@link RepeatOperationsApplicationEvent} for open and
* close we log the event at INFO level and send a JMX notification if we
* are also an MBean.
*
* @see org.springframework.batch.execution.launch.SimpleJobLauncher#onApplicationEvent(org.springframework.context.ApplicationEvent)
*/
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if (applicationEvent instanceof SimpleMessageApplicationEvent) {
String message = applicationEvent.toString();
logger.info(message);
publish(message);
}
return;
}
/**
* Publish the provided message to an external listener if there is one.
*
* @param message the message to publish
*/
private void publish(String message) {
if (notificationPublisher != null) {
Notification notification = new Notification("JobExecutionApplicationEvent", this, notificationCount++,
message);
/*
* We can't create a notification with a null source, but we can set
* it to null after creation(!). We want it to be null so that
* Spring will replace it automatically with the ObjectName (in
* ModelMBeanNotificationPublisher).
*/
notification.setSource(null);
notificationPublisher.sendNotification(notification);
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample.advice;
import org.aspectj.lang.JoinPoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
/**
* Wraps calls for 'Processing' methods which output a single Object to write
* the string representation of the object to the log.
*
* @author Lucas Ward
*/
public class MethodExecutionApplicationEventAdvice implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
/*
* (non-Javadoc)
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
*/
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public void before(JoinPoint jp) {
String msg = "Before: "+jp.toShortString();
publish(jp.getTarget(), msg);
}
public void after(JoinPoint jp) {
String msg = "After: "+jp.toShortString();
publish(jp.getTarget(), msg);
}
public void onError(JoinPoint jp, Throwable t) {
String msg = "Error in: "+jp.toShortString()+"("+t.getClass()+":"+t.getMessage()+")";
publish(jp.getTarget(), msg);
}
/**
* Publish a {@link RepeatOperationsApplicationEvent} with the given
* parameters.
*
* @param context the current batch context
* @param message the message to publish
* @param type the type of event to publish
*/
private void publish(Object source, String message) {
applicationEventPublisher.publishEvent(new SimpleMessageApplicationEvent(source, message));
}
}

View File

@@ -0,0 +1,29 @@
package org.springframework.batch.sample.advice;
import org.springframework.context.ApplicationEvent;
/**
* @author Dave Syer
*
*/
public class SimpleMessageApplicationEvent extends ApplicationEvent {
private String message;
/**
* @param source
* @param message
*/
public SimpleMessageApplicationEvent(Object source, String message) {
super(source);
this.message = message;
}
/* (non-Javadoc)
* @see java.util.EventObject#toString()
*/
public String toString() {
return "message=["+message+"], " + super.toString();
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.batch.sample.tasklet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.repeat.ExitStatus;
@@ -30,6 +32,7 @@ import org.springframework.batch.repeat.ExitStatus;
public class InfiniteLoopTasklet implements Tasklet {
private int count = 0;
private static final Log logger = LogFactory.getLog(InfiniteLoopTasklet.class);
/**
*
@@ -39,9 +42,16 @@ public class InfiniteLoopTasklet implements Tasklet {
}
public ExitStatus execute() throws Exception {
Thread.sleep(500);
count++;
return ExitStatus.CONTINUABLE;
while(true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Job interrupted.");
}
count++;
logger.info("Executing infinite loop, at count="+count);
}
}
}