环境Weblogic 8.1 + JDK 1.4
业务:从数据库中取出需要发送邮件的数据(sendFlag = 'N'),逐条发送邮件,发送完毕后,更新数据库中的标志(sendFlag = 'Y'),大概代码如下:
- package processes.YYYYYMail;
- import com.bea.jpd.JpdContext;
- import com.bea.data.RawData;
- import com.bea.xml.XmlObject;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import processes.EPSendErrorMail;
- /**
- * @jpd:process process::
- * <process name="YYYYYMail">
- * <onException name="OnException">
- * <perform name="onException" method="onException"/>
- * </onException>
- * <clientRequest name="Subscription" method="subscription"/>
- * <perform name="perform" method="perform"/>
- * </process>::
- * @jpd:xquery prologue::
- */
- public class YYYYYMail implements com.bea.jpd.ProcessDefinition
- {
- static final long serialVersionUID = 1L;
- public Callback callback;
- /*
- * The area above this comment contains Variable Declarations created from the Design View.
- * The area below this comment contains Control Declarations created from the Design View.
- *
- * Code in these areas is generated automatically.
- *
- * 2-Way editing is fully supported in these areas of code.
- * Warning: changing Process Variables and Control Declarations already in use by your application
- * may generate errors in your application if you do not update these declarations
- * in all locations where they are used.
- */
- /**
- * @common:control
- */
- private processes.YYYYYMail.DBControl dbControl;
- /**
- * @common:control
- */
- private processes.YYYYYMail.MailControl mailCrontrol;
- /**
- * @common:context
- */
- JpdContext context; //(..control insertion marker - do not modify this line)
- /*
- * The area below this comment contains java methods referenced by communication nodes in the Process Language.
- *
- * Code in this area is generated automatically.
- *
- * 2-Way editing is partially supported in this area of code.
- * Warning: you can safely add code inside a method body but it must be outside of any PROTECTED SECTION
- * block comments. Modifications to code within these comments will prevent you from viewing information
- * in the Design View builder that generated this code.
- */
- /**
- * @jpd:mb-static-subscription message-body="{x0}" channel-name="/XXXX/YYYYYMailTimerChannel"
- */
- public void subscription(java.lang.String x0)
- {
- //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
- // input transform
- // parameter assignment
- //#END : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
- }
- public interface Callback //(..region end marker - do not modify this line)
- {
- }
- /*
- * Code inserted below this comment is for methods corresponding to Perform or Condition nodes
- * created in the Design View.
- *
- * Feel free to make modifications or add new code here.
- */
- public void perform() throws Exception
- {
- HashMap[] hmMailList = this.dbControl.getMailData();
- if(hmMailList == null || hmMailList.length == 0){
- return;
- }
- this.log("Mail count", hmMailList.length + "");
- String YYYYYMailTo = dbControl.getParaValue();
- ArrayList mailTo = new ArrayList();
- mailTo.add(YYYYYMailTo);
- Mail mail = new Mail();
- for(int i = 0; i < hmMailList.length; i ++){
- String mailSubject = formatObject(hmMailList[i].get("CREATE_DATETIME")) + "";
- StringBuffer mailBody = new StringBuffer();
- //...........................
- mailBody.append("建立人员:" + formatObject(hmMailList[i].get("CREATE_USER")));
- this.log((i + 1) + " mailSubject", mailSubject);
- this.log((i + 1) + " mailBody", mailBody.toString());
- mail.sendMultiMail(mailTo, mailSubject, mailBody.toString());
- dbControl.updateSendFlag(formatObject(hmMailList[i].get("SEQ_NO")));
- }
- }
- private void log(String item, String value){
- SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
- StringBuffer sb = new StringBuffer();
- sb.append(sdf.format(new Date()))
- .append("[XXXX][" + item + "=" + value + "]");
- System.out.println(sb.toString());
- }
- private String formatObject(Object o){
- if(o == null){
- return "";
- }
- return ((String)o).trim();
- }
- public void onException() throws Exception
- {
- //出错时,则发送Email
- new EPSendErrorMail().sendErrorMail(context);
- }
- }
打包后发布到 Weblogic Integration上,配置该JPD每2 second运行一次;
在数据库中准备好两条记录(即需要发送两封Mail)
问题现象:
发送发出了三封邮件,其中要两封是一样的。
问题分析:
调试发现,因该JPD每2s运行一次,在上一次运行还没有结束的时候,下一次已经开始运行,结果导致后面一次运行取到的数据是上一次运行准备处理的数据,于是就出现了重复的邮件;
简单解决方法:
将运行时间间隔加长,确保每次运行时,上一次已经结束。