2014/07/11

Automatically Retry Failed Jobs in Quartz


Source: Automatically Retry Failed Jobs in Quartz

Retrying continuously until success:
If you want to keep trying over and over again until the job succeeds, all you have to do is throw a JobExecutionException with a flag to tell the scheduler to fire it again when it fails. The following code shows how:


class MyJob implements Job {
 
  public MyJob() {
  }
 
  public void execute(JobExecutionContext context)
                  throws JobExecutionException {
    try{
        //do something
    }
    catch(Exception e){
 
        Thread.sleep(10000); //sleep for 10 secs
 
        JobExecutionException e2 = new JobExecutionException(e);
        //fire it again
        e2.refireImmediately();
        throw e2;
    }
  }
}
       
 

Retrying n times:
It gets a bit more complicated if you want to retry a certain number of times only. You have to use a StatefulJob and hold a retryCounter in its JobDataMap, which you increment if the job fails. If the counter exceeds the maximum number of retries, then you can disable the job if you wish.




class MyJob implements StatefulJob {
 
  public MyJob() {
  }
 
  public void execute(JobExecutionContext context)
                                 throws JobExecutionException {
    JobDataMap dataMap = context.getJobDetail().getJobDataMap();
    int count = dataMap.getIntValue("count");
 
    // allow 5 retries
    if(count >= 5){
        JobExecutionException e = new JobExecutionException("Retries exceeded");
        //unschedule it so that it doesn't run again
        e.setUnscheduleAllTriggers(true);
        throw e;
    }
 
    try{
        //do something
 
        //reset counter back to 0
        dataMap.putAsString("count", 0);
    }
    catch(Exception e){
        count++;
        dataMap.putAsString("count", count);
        JobExecutionException e2 = new JobExecutionException(e);
 
        Thread.sleep(10000); //sleep for 10 secs
 
        //fire it again
        e2.refireImmediately();
        throw e2;
    }
  }
}
       
 

2014/05/03

Admob廣告收入


Admob廣告收入構成
收入=廣告展示量/1,000 * 千次展示收入
千次展示收入=點擊率 * 廣告單價
廣告單價,台灣大概0.1 USD,各國不一定
假設一個月
廣告展示量=100,000 次
點擊率 = (0.1%) 0.001
廣告收入=100000/1000*0.001*0.1=0.01USD
但是其他報告是說,
行動裝置平均每次點擊約有0.01 USD,一般網頁平均每次點擊約有0.1 USD
這報告告訴大家,錢真的不好賺啊 QQ

2013/02/28

PostgreSQL Binary Install in Win7



  • download binary files: http://www.enterprisedb.com/products-services-training/pgbindownload
  • unzip to c:\pgsql
  • create a batch file: setevn.bat:
      set PGHOME=C:\pgsql
      set PATH=%PGHOME%\bin;%path%
      set PGHOST=localhost
      set PGLIB=%PGHOME%\lib
      set PGDATA=%PGHOME%\data
  • as a system administrator to run the "command prompt"
  • execute the following script:
  •           C:\>cd C:\pgsql
      C:\pgsql>setenv.bat
      C:\pgsql>initdb.exe -D %PGDATA% -E UTF8 --locale=C
      C:\pgsql>pg_ctl.exe register -D %PGDATA% -N "PostgreSQL 918"

2012/12/26

SQL Server Table Schema 查詢與欄位定序



/*資料表*/
Select * From SysObjects Where xType='U' Order By Name

/*欄位*/
Select * From SysObjects A Inner Join SysColumns B On A.ID=B.ID Where A.xType='U' Order By A.Name,ColID

/*讀取SQL 資料表欄位結構的SQL 語法*/
Select A.Name As TableName,B.ColOrder As ColOrder,B.Name As ColName,C.Name As ColType,B.Length As ColLen,B.XPrec As ColPrecision,B.XScale As ColScale
From (SysObjects A Inner Join SysColumns B On A.ID=B.ID) Inner Join SysTypes C On B.XType=C.XType
Where A.XType='u'
Order By A.Name,B.ColOrder

/*修改欄位定序*/
ALTER TABLE MyTable ALTER COLUMN CharCol
varchar(10) COLLATE Chinese_Taiwan_Stroke

2012/11/13

reverse proxy without apache http server



在 $tomcat_home\conf\server.xml 中修改以下內容

 

<host appbase="" name="localhost" p="">unpackWARs="true" autoDeploy="true"  
 xmlValidation="false" xmlNamespaceAware="false"&gt;  
 <valve classname="org.apache.catalina.valves.AccessLogValve" directory="logs" p="">prefix="appName_access_log." suffix=".txt"  
 pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/&gt;  
 <context docbase="webapps/appName" path="" reloadable="false"></context>  
 </valve>
</host>  

2012/11/12

Amazon EC2 Running Tomcat on port 80

SSH command:

#sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

#sudo /sbin/service iptables save

server.xml:


#service tomcat7 restart

2012/10/18

SSH in CentOS 6.3



SSH 安裝
#sudo yum  install openssh-server openssh-clients

SSH 開機時啟動
#sudo /sbin/chkconfig sshd on

SSH 啟動
#sudo /sbin/service sshd start

防火牆設定
#sudo vi /etc/sysconfig/iptables

添加下面一行(範例 port = 22)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

重新啟動防火牆
#sudo /sbin/service iptables stop
#sudo /sbin/service iptables start

調整 SSH 設定
#vi  /etc/ssh/sshd_config

禁止 root 通過 SSH 登入
PermitRootLogin no

重新啟動

NGINX SSL/設定檔案

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #...