2015年11月7日 星期六

該如何使用Spring boot和Java annotation寫排程(Scheduler)

在文章中說明該如何使用Java寫排程

一、使用的技術:
  • Gradle
  • Java annotation
  • Spring Boot
二、Gradle使用的Library
dependencies {
  compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
  compile 'org.quartz-scheduler:quartz:2.2.2'
  compile 'org.springframework:spring-context-support:4.1.8.RELEASE'
}
三、開始寫程式

首先建置Service,定義將在排程中執行的類別和方法,
在此範例中我定義了一個打招呼以及定期print時間的功能。

package app.service;

import java.util.Calendar;

public class HelloService {
 
 public void Hello(){
  System.out.println("Hello!Jason");
  System.out.println(Calendar.getInstance().getTime());
 }
}

接著建置一個SchedulConfig,
使用Java annotation 的方式來產生(管理)Spring製造的bean。

package app.config;

import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class SchedulConfig {
 
 @Bean //1.將HelloService納入Spring來管理
 public HelloService helloService(){
  return new HelloService();
 }
 
 @Bean
 @Autowired //2.建立helloJob,將HelloService注入,指定執行的方法名稱
 public MethodInvokingJobDetailFactoryBean helloJob(HelloService helloService){
  MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
  bean.setTargetObject(helloService);
  bean.setTargetMethod("Hello");

  return bean;
 }
 
 @Bean
 @Autowired //3.建立helloTrigger,將helloJob注入,指定執行的時間,參數依序是 秒 分 時 ...etc
 public CronTriggerFactoryBean helloTrigger(MethodInvokingJobDetailFactoryBean helloJob){
  CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
  bean.setJobDetail(helloJob.getObject());
  bean.setCronExpression("0/10 * * * * ? ");
  return bean;
 }
 
 @Bean
 @Autowired //4.建立scheduler,可執行多個Trigger
 public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean helloTrigger){
  SchedulerFactoryBean bean = new SchedulerFactoryBean();

  Trigger[] triggers = {helloTrigger.getObject()};
  bean.setTriggers(triggers);
  return bean;
 }
 
}


接著Spring boot執行程式的方式如下:
package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

 public static void main(String[] args) throws Exception  {
  // TODO Auto-generated method stub
  SpringApplication.run(Application.class, args);
 }

}

使用eclipse直接run application或是export jar執行都可以喔XDDDD

程式結果:
Hello!Jason
Sat Nov 07 22:25:30 CST 2015
Hello!Jason
Sat Nov 07 22:25:40 CST 2015
Hello!Jason
Sat Nov 07 22:25:50 CST 2015
Hello!Jason
Sat Nov 07 22:26:00 CST 2015
...

2015年10月31日 星期六

JasperReport產生PDF報表列印罕用字(外字)問題

在工作上用Java產生動態資料的PDF報表,
遇到有罕用字(外字)無法列印的問題,
原因是該(.ttf)字體沒有這個字的緣故,
在這邊做個筆記,
若是外字不多的情況,可以使用該方法應急。
P.S.在以下的內容中將以(外字 = 罕用字)說明之。

一、首先文章會出現的工具如下:
JasperReport:Java產生報表的lib。
iReport:設計報表樣式的工具。
fontforge:字體(.ttf)整合(merge)工具。(教學可參考:http://blog.twtnn.com/2015/01/svg12.html 

二、問題描述:
  1. iReport須設定pdf所需要使用的字體,我使用的是標楷體(kaiu.ttf)。
  2. 該外字在標楷體(kaiu.ttf)中並無此字型。
  3. 驗證方法係開啟Word,將外字複製過去選擇標楷體,會把字體強制轉為細明體-ExtB。
  4. 在iReport中不方便設定細明體-ExtB(mingliub.ttc),原因如下:
  • JasperReport對(.ttc)的字體支援是有問題的。
           (若是有成功的大大煩請賜教XD)
  • 將該(.ttc)轉成(.ttf)也沒有辦法,因為該字體只儲存外字,這樣一般字就會無法顯示。

三、解決方式:
一般字的.ttf與外字的.ttf合併起來,
此方式須將所需外字一個個字合併。
(慶幸我目前只需處理五個字.......XD)
我所合併的字體是,
全字庫正宋體 Ext-B(TW-Sung-Ext-B-98_1.ttf)併入標楷體
之後只要將merge後的標楷體放入程式所需位置,
就可以產生出外字了。

四、步驟:
  1. 下載全字庫字體(位置:http://data.gov.tw/node/5961 )。
  2. 安裝 fontforge (位置:http://fontforge.github.io/en-US/downloads/windows/)。
  3. 開啟 fontforge 以及 全字庫正宋體 Ext-B (TW-Sung-Ext-B-98_1.ttf)
  4. 按下ctrl+shift+> 找出外字的Unicode or Decimal。
  5. 我係先用java轉出外字的Unicode,再使用網站轉換成Decimal搜尋(網站:https://www.branah.com/unicode-converter )。
  6. 在依照該網站上的教學http://blog.twtnn.com/2015/01/svg12.html 將字體一個個貼到標楷體,接著再匯出(.ttf)就行了。



2015年10月28日 星期三

I wish...

最近覺得表達的能力越來越弱,
決定開始寫網誌,
精進自己的表達能力,
這個網誌會記錄我的生活,
將程式所學做個筆記,
做一個較有系統性的學習,
我期許自己能夠成為一個樂於分享的人,
如果我能夠勇於表達自己的想法,
是否就不會再被恐懼的浪潮吞噬?
成為一個有信心的人;)