设计模式 之 模板模式

 

 

 

 

public interface ISendXTask extends Runnable{
	public void startTask();
}

 

public abstract class AbstractSendXTask<T extends BaseEntity> implements ISendXTask { 
	
	private boolean isRunning = false;
 
	private Hashtable<Integer, Message> cache = new Hashtable<Integer, Message>();

	public void startTask()
	{
		if (isRunning)
		{ 
			return;
		}
		try
		{
			isRunning = true;
			run();
		}
		catch (Exception e)
		{ 
		}
		finally
		{
			isRunning = false;
		}
	}

	@Override
	public void run()
	{
		List<T> list = getNeedSendList();
		if (list != null && list.size() > 0)
		{ 
			for (T t : list)
			{
				try
				{
					processOne(t);
				}
				catch (QueueFullException e)
				{
					try
					{
						Thread.sleep(20000);
					}
					catch (InterruptedException e1)
					{
						e1.printStackTrace();
					}
					break;
				}
			}
		}
	}
 
	public void processOne(T t) throws QueueFullException
	{
		preSend(t);
		if (t.getInsertTime() != null && isOvertime(t)) 
			sendFailedProcess(t);
		else
			try
			{
				if(send(t))
					sendSuccessProcess(t);
				else
					sendFailedProcess(t);
			}
			catch (QueueFullException e)
			{
				throw e;
			}
			catch (Exception e)
			{ 
				sendFailedProcess(t);
			}
	}

	public boolean isOvertime(T t)
	{
		return (System.currentTimeMillis() - t.getInsertTime().getTime() > getOvertime());
	}
 
	public long getOvertime()
	{
		return 5*60*1000l;
	}
 
//	public Message findMessage(int msgId)
//	{
//		Message msg = null;
//		msg = cache.get(msgId);
//		if (msg == null)
//		{
//			msg = dao.find(msgId);
//			if (msg != null)
//			{
//				if (cache.size() > 100)
//					cache.clear();
//				cache.put(msg.getMsgId(), msg);
//			}
//		}
//		return msg;
//	}

 
	public void preSend(T t)
	{
	}
 
	public abstract List<T> getNeedSendList(); 
	public abstract boolean send(T pkg) throws QueueFullException; 
	public abstract void sendSuccessProcess(T t); 
	public abstract void sendFailedProcess(T t);

 

public class BaseEntity {

	
	public void setInsertTime(Date date)
	{
	}

	public Date getInsertTime()
	{
		return null;
	}
	
	
}

 

public class AndroidMessage extends BaseEntity{

}

 

public class IOSMessage extends BaseEntity{

}

 

package 设计模式.模板模式;

import java.util.List;

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月8日 下午12:18:11
 * 类说明:
 */
public class AndroidTask extends AbstractSendXTask<AndroidMessage>{

	@Override
	public List<AndroidMessage> getNeedSendList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean send(AndroidMessage pkg) throws QueueFullException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void sendSuccessProcess(AndroidMessage t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sendFailedProcess(AndroidMessage t) {
		// TODO Auto-generated method stub
		
	}

}

 

package 设计模式.模板模式;

import java.util.List;

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月8日 下午12:18:11
 * 类说明:
 */
public class IOSTask extends AbstractSendXTask<IOSMessage>{

	@Override
	public List<IOSMessage> getNeedSendList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean send(IOSMessage pkg) throws QueueFullException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void sendSuccessProcess(IOSMessage t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sendFailedProcess(IOSMessage t) {
		// TODO Auto-generated method stub
		
	}

}

 

 

 

 

 

 

你可能感兴趣的:(设计模式,模板模式)