lazyload the images

最新精選 Featured post

異世界穿越作品整合

訂閱

訂閱 FB 專頁

每月雙數周日為固定發佈日

訂閱FB 專頁,有新發佈時將會立即看見。
Youtube 頻道經常會有電玩錄影

01/08/2019

PIMS implement scheduler in java

文章分類: , , ,

PIMS 系統 需要定期去抓外匯價格(Forex)的數據,所以便需要一個Scheduler的功能。


背景

  • OS 是 windows 8
  • Apache/2.4.34 (Win32)
  • OpenSSL/1.0.2o
  • PHP/5.6.37
  • PHP extension: mysqli curl mbstring

Scheduler

由於使用windows 可以透過幾種方法實用 schedule task

  1. 使用windows 內置的windows scheduler
  2. 安裝一些PHP framework
  3. 在Tomcat上跑Java

windows scheduler

先寫段script,以.bat檔儲存
例如
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
再以windows scheduler 定期執行
.php 的內容當然是去抓Forex數據

優點︰
  • 不需要額外安裝任何東西
  • 可以執行site root 之外路徑的.php file,避免使用者直接瀏覽獨發scheduler
缺點︰
  • 假若日後將系統搬上互聯網,不可以只租用Web Hosting服務,需要租用 Virtual Private Server (VPS) (虛擬主機) / Cloud Hosting (雲伺服器‎)。
    成本比租用 Web Hosting (網頁寄存) 高
因為Web Hosting 一般只可以選linux / windows,內容即是
linux 即是跑Apache web server
可以支授 PHP5/PHP7, MySQL

windows 即是跑 IIS web server
可以支授 ASP.NET/PHP5/PHP7, MsSQL
你是沒有權力控制OS層面的任何東西,甚至remote 功能也沒有。
一般只透過cPanel / FTPs 對web server進行更新/設定/上傳/下傳

PHP scheduler framework

  • zend
  • laravel
  • Crunz
  • peppeocchi / php-cron-scheduler
  • Cron/Cron
優點︰
  • 部份web hosting 服務內建一些著名的framework,只需一鍵安裝,或容許自行安裝任何framework。租用web hosting便可以在互聯網使用PIMS,減少成本。
  • php 頁內執行scheduler動作,可以透過.htaccess 設定限制,避免使用者直接瀏覽獨發scheduler
缺點︰
  • 增加學習使用個別framework的時間
  • 對framework產生新的依賴,日後更新或維護多一項依賴性需要測試/留意
  • 對framework的依賴可能影響PHP的版本依賴,例如framework只支授PHP5,即使你很想用PHP7的新功能,因為framework在PHP7跑不動,你就不可以上PHP7。

Java app on Tomcat

以Java App,透過 Java 原生支援linux cron job 相同類型功能。
編寫一個Java App,可以定時獨發。
scheduler 的實作方水法可以寫在Java / PHP

優點︰
  • 不少web hosting 隨了apache web server,還附帶tomcat server。不需要租用較高成本的 Virtual Private Server (VPS) (虛模主機) / Cloud Hosting (雲伺服器‎)。
  • Java 是跨平台的,Java社群比PHP 單一framework更大,相對cron job的功能也更穩定和成熟。
缺點︰
  • PIMS增加複雜性,由兩種不同的language組成。

How to implement a scheduler in Java

最後杜芬宅選了Java,相比學習一個新的PHP framework,杜芬宅更熟悉 Java

今次使用了 Apache HttpClient
下載後,將全部.jar 加入到Java Project當中

Add external jars in project

  1. Right-click on your project
  2. Find or Search Java Build Path
  3. Click on "Libraries" Tab

Option 1

  1. Select Add External JARs
  2. Select the jar file from the required folder
  3. Click and Apply and Ok


Option 2

  1. Create a folder called lib in your project folder.
  2. copy all the jar files you need to this folder .
  3. Refresh your project in eclipse.
  4. Click [Add Library] and/or [Add Class Folder...] to include your .jar files


Include external jars in exporting war file

設定當export WAR file時,把 Libraries.jar 也一起export。
不然,Tomcat 執行 WAR file會因為找不到相關參照,會出錯。

  1. Right Click on the project -> properties. On properties window
  2. select "Deployment Assembly" on the left hand side
  3. Click "add"
  4. select "Java Build Path Entries".
  5. Select all the required libraries
  6. Right Click on the project -> export. Select Web -> .war file.
  7. You will find the libraries under /WEB-INF/lib

java send http request with json


杜芬宅已經將 Java source 放上 Github Gist
https://gist.github.com/keithbox/8c5e224fe174514a4cca31692deca31c


How to deploy a Java EE project to tomcat server

寫好後,需要將Java project export成 WAR file
Deploy(部署)到tomcat server中

Option 1
right click on project, select export > WAR file
export the WAR file to the tomcat webapps folder
e.g D:\xampp\tomcat\webapps
the war file will be extracted under webapps folder

validation
access the web application through below URL by the WAR filename
localhost:8080/yourWarFileName

regression
delete the WAR file just copied to the webapps folder, the unzipped folder will be deleted automatically after a moment.

當部署時,建議把Tomcat server關了
複製WAR file後,如發現已解壓縮的WAR file,把整個folder 移除。
再啟動 Tomcat server

Reference

How do I run a PHP script using windows schedule task?
https://stackoverflow.com/questions/4701861/how-do-i-run-a-php-script-using-windows-schedule-task

Zend framework
Scheduling with Zend Job Queue
https://www.sitepoint.com/zend-queue/
Scheduled tasks (cronjob-aternative) with Zend framework?
https://stackoverflow.com/questions/4248320/scheduled-tasks-cronjob-aternative-with-zend-framework
sinhaDroid/Job_queue_step_one_a
https://gist.github.com/sinhaDroid/8c360c88721406b2edcbc7481eb2a2a2

Task Scheduling - Laravel - The PHP Framework For Web Artisans
https://laravel.com/docs/5.8/scheduling

Crunz
https://github.com/lavary/crunz

peppeocchi / php-cron-scheduler
https://github.com/peppeocchi/php-cron-scheduler

Cron/Cron
https://github.com/Cron/Cron

How to schedule a task in Tomcat
https://stackoverflow.com/questions/18226126/how-to-schedule-a-task-in-tomcat/36449830

How can I execute a PHP script from Java?
https://stackoverflow.com/questions/655620/how-can-i-execute-a-php-script-from-java

How to implement a scheduler in Java for send request to PHP
https://gist.github.com/keithbox/8c5e224fe174514a4cca31692deca31c

Database table design for scheduling tasks https://stackoverflow.com/questions/12089431/database-table-design-for-scheduling-tasks

dbo.sysschedules (Transact-SQL) https:https://docs.microsoft.com/en-us/sql/relational-databases/system-tables/dbo-sysschedules-transact-sql?view=sql-server-2017

Cron Expression Generator & Explainer - Quartz https:https://www.freeformatter.com/cron-expression-generator-quartz.html

dragonmantank / cron-expression
https://github.com/dragonmantank/cron-expression

HTTP POST using JSON in Java
https://stackoverflow.com/questions/7181534/http-post-using-json-in-java

Apache HttpComponents – Apache HttpComponents
https://hc.apache.org/downloads.cgi

用httpPost对JSON发送和接收的例子
https://blog.csdn.net/u010355144/article/details/52141173

How to include dependencies in WAR file?
https://stackoverflow.com/questions/10088604/how-to-include-dependencies-in-war-file

Adding an External Library to an Eclipse Project Folder
http://www.oxfordmathcenter.com/drupal7/node/44

How to add all external jars to library on eclipse export to war
https://stackoverflow.com/questions/11918957/how-to-add-all-external-jars-to-library-on-eclipse-export-to-war

No comments:

Post a Comment