Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 International Business Machines Corp.
   * 
   * See the NOTICE file distributed with this work for additional information
   * regarding copyright ownership. 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 com.ibm.jbatch.container.impl;
 
 import java.util.List;
 import java.util.Set;
 
 
 
 public class SkipHandler<T> {

Logic for handling skipped records. A SkipHandler object is attached to every BDS that inherits from AbstractBatchDataStream.
 
 
 	  private static final String className = SkipHandler.class.getName();
 		private static Logger logger = Logger.getLogger(SkipHandler.class.getPackage().getName());
 
 	  public static final String SKIP_COUNT      = "skip-limit";
 	  public static final String SKIP_INCLUDE_EX = "include class";
 	  public static final String SKIP_EXCLUDE_EX = "exclude class";
 
 	  private List<SkipReadListenerProxy_skipReadListener = null;
 
 	  private long _jobId = 0;
 	  private String _stepId = null;
 	  private Set<String_skipIncludeExceptions = null;
 	  private Set<String_skipExcludeExceptions = null;
 	  private int _skipLimit = 0;
 	  private long _skipCount = 0;
 	  private Exception _skipException = null;
 
 
 	  public SkipHandler(Chunk chunklong lString stepId)
 	  {
 	     = l;
 	     = stepId;
 
 	    initialize(chunk);
 	  }


  
Add the user-defined SkipReadListeners.
 
 	  public void addSkipReadListener(List<SkipReadListenerProxyskipReadListener)
 	  {
 	     = skipReadListener;
 	  }
  
  
Add the user-defined SkipWriteListeners.
 
 	  public void addSkipWriteListener(List<SkipWriteListenerProxyskipWriteListener)
 	  {
 	     = skipWriteListener;
 	  }
  
  
Add the user-defined SkipReadListeners.
 
 	  public void addSkipProcessListener(List<SkipProcessListenerProxyskipProcessListener)
 	  {
 	     = skipProcessListener;
 	  }


  
Read the skip exception lists from the BDS props.
	  private void initialize(Chunk chunk)
	  {
	    final String mName = "initialize";
	      .entering(mName);
	    try
	    {
	    	if (chunk.getSkipLimit() != null){
	    		 = Integer.parseInt(chunk.getSkipLimit());
	    	}
	    }
	    catch (NumberFormatException nfe)
	    {
	      throw new RuntimeException("NumberFormatException reading " + nfe);
	    }
	    if ( > 0)
	    {
	      // Read the include/exclude exceptions.
	  
	       = new HashSet<String>();
	       = new HashSet<String>();
	      //boolean done = false;
	      List<StringincludeEx = new ArrayList<String>();
	      List<StringexcludeEx = new ArrayList<String>();
	      
			if (chunk.getSkippableExceptionClasses() != null) {
				if (chunk.getSkippableExceptionClasses().getIncludeList() != null) {
					for (ExceptionClassFilter.Include include : includes){
						.finer("SKIPHANDLE: include: " + include.getClazz().trim());
					}
						.finer("SKIPHANDLE: include element not present");
					}
					//if (includes.size() > 1) {
					//	String msg = "TODO: Do not currently support >1 <include> element, even though spec allows this.";
					//	logger.severe(msg);
					//	throw new IllegalArgumentException(msg);
					//} else if (includes.size() == 1) {
					//	includeEx = includes.get(0).getClazz();
					//	logger.finer("SKIPHANDLE: include: " + includeEx);
					//}  else {
					//	logger.finer("SKIPHANDLE: include element not present");
					//}
				}
			}
			if (chunk.getSkippableExceptionClasses() != null) {
				if (chunk.getSkippableExceptionClasses().getExcludeList() != null) {
					for (ExceptionClassFilter.Exclude exclude : excludes){
						.finer("SKIPHANDLE: exclude: " + exclude.getClazz().trim());
					}
						.finer("SKIPHANDLE: exclude element not present");
					}
					//if (excludes.size() > 1) {
					//	String msg = "TODO: Do not currently support >1 <exclude> element, even though spec allows this.";
					//	logger.severe(msg);
					//	throw new IllegalArgumentException(msg);
					//} else if (excludes.size() == 1) {
					//	excludeEx = excludes.get(0).getClazz();
					//	logger.finer("SKIPHANDLE: exclude: " + excludeEx);
					//}  else {
					//	logger.finer("SKIPHANDLE: exclude element not present");
					//}
				}
			}
			//if (includeEx != null)
			//	_skipIncludeExceptions.add(includeEx.trim());
			//if (excludeEx != null)
			//	_skipExcludeExceptions.add(excludeEx.trim());
			//done = (includeEx == null && excludeEx == null);
						"added include exception " + includeEx
"; added exclude exception " + excludeEx);
		}
	        
	      .exiting(mNamethis.toString());
	  }


  
Handle exception from a read failure.
	  public void handleExceptionRead(Exception e)
	  {
	    final String mName = "handleException";
	    
	    .finer("SKIPHANDLE: in skiphandler handle exception on a read");
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipReadListenerProxy skipReadListenerProxy : ) {
	    		  skipReadListenerProxy.onSkipReadItem(e);
				}
	      }
	    }
	    else
	    {
	      // No skip.  Throw it back. don't throw it back - we might want to retry ...
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow"e);
	      	throw new BatchContainerRuntimeException(e);
	    }
	      .exiting(mNamee);
	  }

  
Handle exception from a process failure.
	  {
	    final String mName = "handleExceptionWithRecordProcess";
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipProcessListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipProcessListenerProxy skipProcessListenerProxy : ) {
	    		  skipProcessListenerProxy.onSkipProcessItem(we);
				}
	      }
	    }
	    else
	    {
	      // No skip.  Throw it back.
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow "e);
	      throw new BatchContainerRuntimeException(e);
	    }
	  }
  
Handle exception from a write failure.
	  public void handleExceptionWithRecordListWrite(Exception eList<T> items)
	  {
	    final String mName = "handleExceptionWithRecordListWrite(Exception, List<T>)";
	    
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipWriteListenerProxy skipWriteListenerProxy : ) {
	    		  skipWriteListenerProxy.onSkipWriteItem(itemse);
				}
	      }
	    }
	    else
	    {
	      ..println("## NO SKIP");
	      // No skip.  Throw it back. - No, exit without throwing
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow "e);
	      throw new BatchContainerRuntimeException(e);
	    }
	  }


  
Check the skipCount and skippable exception lists to determine whether the given Exception is skippable.
	  private boolean isSkippable(Exception e)
	  {
	    final String mName = "isSkippable";
	    String exClassName = e.getClass().getName();
	    if(.isLoggable(.)) 
	      .logp(.mNamemName + ": " + retVal + ": " + exClassName);
	    return retVal;
	  }

  
Check whether given exception is in skippable exception list
	  private boolean containsSkippable(Set<StringskipListException e)
	  {
	    final String mName = "containsSkippable";
	    boolean retVal = false;
	    for ( Iterator it = skipList.iterator(); it.hasNext(); ) {
	        String exClassName = (Stringit.next();   
	        try {
	            ClassLoader tccl = Thread.currentThread().getContextClassLoader();	            
	        	if (retVal = tccl.loadClass(exClassName).isInstance(e))
	        		break;
	        } catch (ClassNotFoundException cnf) {
	        	.logp(.mNamecnf.getLocalizedMessage());
	        }
	    }
	    if(.isLoggable(.)) 
	      .logp(.mNamemName + ": " + retVal );
	    return retVal;
	  }
  

  
Check if the skip limit has been reached. Note: if skip handling isn't enabled (i.e. not configured in xJCL), then this method will always return TRUE.
	  private boolean isSkipLimitReached()
	  {
	    return ( >= );
	  }
	  
	  private void logSkip(Exception e)
	  {
	    String key = "record.skipped.by.batch.data.stream";
	    Object[] details = { e.getClass().getName() + ": " + e.getMessage() };
	    //String message = LoggerUtil.getFormattedMessage(key, details, true);
	    //logger.info(message);	
	    //ServicesManager.getInstance().getJobLogManagerService(_jobId).println(message);
		}
	  public long getSkipCount()
	  {
	    return ;
	  }
	  public void setSkipCount(long skipCount)
	  {
	    final String mName = "setSkipCount";
	     = skipCount;
	    if(.isLoggable(.)) 
	      .logp(.mName"setSkipCount: " + );
	  }
	  public String toString()
	  {
	    return "SkipHandler{" + super.toString() + "}count:limit=" +  + ":" + ;
	  }
New to GrepCode? Check out our FAQ X