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

@@ -1,100 +0,0 @@
/*
* 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.execution.bootstrap;
import javax.management.Notification;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.repeat.interceptor.RepeatOperationsApplicationEvent;
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 RepeatOperationsApplicationEvent) {
RepeatOperationsApplicationEvent event = (RepeatOperationsApplicationEvent) applicationEvent;
int type = event.getType();
if (type == RepeatOperationsApplicationEvent.OPEN
|| type == RepeatOperationsApplicationEvent.CLOSE
|| type == RepeatOperationsApplicationEvent.ERROR) {
String message = event.getMessage() + "; source="
+ event.getSource();
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(
"RepeatOperationsApplicationEvent", 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

@@ -67,7 +67,7 @@ public class SimpleJob extends AbstractJob {
// The job was already stopped before we even got this far. Deal
// with it in the same way as any other interruption.
if (execution.getStatus() == BatchStatus.STOPPED) {
if (execution.getStatus() == BatchStatus.STOPPING) {
throw new JobInterruptedException("JobExecution already stopped before being executed.");
}
@@ -126,7 +126,7 @@ public class SimpleJob extends AbstractJob {
rethrow(t);
}
finally {
execution.setEndTime(new Date(System.currentTimeMillis()));
execution.setEndTime(new Date());
execution.setExitStatus(status);
jobRepository.saveOrUpdate(execution);
}

View File

@@ -86,6 +86,9 @@ public class TaskletStep implements Step, InitializingBean, BeanNameAware {
this.name = name;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.Step#getStartLimit()
*/
public int getStartLimit() {
return this.startLimit;
}
@@ -99,6 +102,9 @@ public class TaskletStep implements Step, InitializingBean, BeanNameAware {
this.startLimit = startLimit;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.Step#isAllowStartIfComplete()
*/
public boolean isAllowStartIfComplete() {
return this.allowStartIfComplete;
}

View File

@@ -1,66 +0,0 @@
/*
* 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.execution.bootstrap;
import java.util.ArrayList;
import java.util.List;
import javax.management.Notification;
import org.springframework.batch.repeat.interceptor.RepeatOperationsApplicationEvent;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.UnableToSendNotificationException;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class JobExecutionNotificationPublisherTests extends TestCase {
JobExecutionNotificationPublisher publisher = new JobExecutionNotificationPublisher();
public void testRepeatOperationsBeforeNotUsed() throws Exception {
final List list = new ArrayList();
publisher.setNotificationPublisher(new NotificationPublisher() {
public void sendNotification(Notification notification)
throws UnableToSendNotificationException {
list.add(notification);
}
});
publisher.onApplicationEvent(new RepeatOperationsApplicationEvent(this,
"foo", RepeatOperationsApplicationEvent.BEFORE) {
});
assertEquals(0, list.size());
}
public void testRepeatOperationsOpenUsed() throws Exception {
final List list = new ArrayList();
publisher.setNotificationPublisher(new NotificationPublisher() {
public void sendNotification(Notification notification)
throws UnableToSendNotificationException {
list.add(notification);
}
});
publisher.onApplicationEvent(new RepeatOperationsApplicationEvent(this,
"foo", RepeatOperationsApplicationEvent.OPEN));
assertEquals(1, list.size());
assertEquals("foo", ((Notification) list.get(0)).getMessage()
.substring(0, 3));
}
}

View File

@@ -65,7 +65,7 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
assertEquals(job, firstExecution.getJobInstance().getJob());
jobRepository.saveOrUpdate(firstExecution);
firstExecution.stop();
firstExecution.setEndTime(new Date());
jobRepository.saveOrUpdate(firstExecution);
JobExecution secondExecution = jobRepository.createJobExecution(job, jobParams);
@@ -84,7 +84,7 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
JobParameters jobParams = new JobParameters();
JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams);
firstExecution.stop();
firstExecution.setEndTime(new Date());
jobRepository.saveOrUpdate(firstExecution);
JobExecution secondExecution = jobRepository.createJobExecution(job, jobParams);