跳到主要內容

IReport中的條碼類別BcImage

最近有一個繳費單的專案,需要列印條碼,因為IReport本身就有提供列印條碼的功能,所以就用IReport設計繳費單然後再用Jasperreport API寫批次程式去產生PDF,資料量大概3000多筆(頁)拆成幾個檔案,可是就發生了一個問題居然發生部分繳費單的條碼有問題,看了一下程式研判是Race Condition因為程式採用的是多執行緒,若是依序個別產生是不會有問題,但是同時執行的時候就會亂掉,而且都錯在條碼,令我覺得很奇怪,後來我查了一下發現it.businesslogic.ireport.barcode.BcImage 類別我猜是這裡的問題,就先加上synchronized重新編譯在去執行,嘿嘿條碼就對了,做了一下壓測也正常,程式碼如下,不過這樣事不是對了可能還要仔細查查看。

public class BcImage {
private static net.sourceforge.barbecue.Barcode bc = null;

public synchronized static net.sourceforge.barbecue.Barcode getBarcode() {
return bc;
}

public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum) {
return getBarcodeImage(type, aText, showText, checkSum,"",0,0);
}

public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum, String applicationIdentifier, int width, int height) {
// 2of7, 3of9, Bookland, Codabar, Code128, Code128A, Code128B, Code128C, Code39, EAN128, EAN13, GlobalTradeItemNumber, Int2of5, Int2of5, Monarch, NW7, PDF417, SCC14ShippingCode, ShipmentIdentificationNumber, SSCC18, Std2of5, Std2of5, UCC128, UPCA, USD3, USD4, USPS

String text = new StringBuffer().append(aText).toString();

try {
switch (type) {
case 0: return null;
case 1: bc = BarcodeFactory.create2of7(text); break;
case 2: bc = BarcodeFactory.create3of9(text, checkSum); break;
case 3: bc = BarcodeFactory.createBookland(text); break;
case 4: bc = BarcodeFactory.createCodabar(text); break;
case 5: bc = BarcodeFactory.createCode128(text); break;
case 6: bc = BarcodeFactory.createCode128A(text); break;
case 7: bc = BarcodeFactory.createCode128B(text); break;
case 8: bc = BarcodeFactory.createCode128C(text); break;
case 9: bc = BarcodeFactory.createCode39(text, checkSum ); break;
case 10: bc = BarcodeFactory.createEAN128(text); break;
case 11: bc = BarcodeFactory.createEAN13(text); break;
case 12: bc = BarcodeFactory.createGlobalTradeItemNumber(text); break;
case 13: bc = BarcodeFactory.createInt2of5(text, checkSum); break;
case 14: bc = BarcodeFactory.createMonarch(text); break;
case 15: bc = BarcodeFactory.createNW7(text); break;
case 16: bc = BarcodeFactory.createPDF417(text); break;
case 17: bc = BarcodeFactory.createSCC14ShippingCode(text); break;
case 18: bc = BarcodeFactory.createShipmentIdentificationNumber(text); break;
case 19: bc = new UCCEAN128Barcode(UCCEAN128Barcode.SSCC_18_AI, text, checkSum); break; //BarcodeFactory.createSSCC18(text); break;
case 20: bc = BarcodeFactory.createStd2of5(text, checkSum); break;
case 21: bc = new UCCEAN128Barcode(applicationIdentifier, text, checkSum); break; //BarcodeFactory.createUCC128(applicationIdentifier, text); break;
case 22: bc = BarcodeFactory.createUPCA(text); break;
case 23: bc = BarcodeFactory.createUSD3(text, checkSum); break;
case 24: bc = BarcodeFactory.createUSD4(text); break;
case 25: bc = BarcodeFactory.createUSPS(text); break;
case 26: bc = new net.sourceforge.barbecue.linear.code39.Code39Barcode(text, checkSum, true); break;
}

if (width > 0) bc.setBarWidth(width);
if (height > 0) bc.setBarHeight(height);
bc.setDrawingText(showText);
return net.sourceforge.barbecue.BarcodeImageHandler.getImage(bc);
}
catch (Exception e) {
e.printStackTrace();
//generate a runtime exception, invalid value passed.
//the user must be notified if fail
throw new RuntimeException(e.getMessage());
//return null;
}
}
}

留言

這個網誌中的熱門文章

Jasig CAS SSO 安裝篇

最近在玩SSO CAS,Open Source的Jasig CAS我覺得很好入手,之後就慢慢介紹囉。 先以Tomcat 7.0.37為Ap Server, OS:Window。 先安裝JDK及安裝 Tomcat 。 再下載 Jasig CAS Server ,並解開壓縮檔。 解開後會有很多目錄,都是cas-server-xxxx這些都是source,若是有需求可以自行修改,然後用Maven修改。本次因為只有安裝所以就用modules的目錄就可以囉,複製cas-server-webapp-x.x.x.war到Tomcat\webapps目錄下。 先啟動Tomcat,正常的話應該是可以正常啟動,然後打開Browser輸入http://hostname:8080/cas-server-webapp-x.x.x/login,應該會看到登入畫面但是有警示,你要使用SSL表示快成功了。

Spring boot v1.5 (六) spring data jpa 基本操作

最近天氣好熱,做甚麼事都覺得很懶,想要寫個spring data jpa也是懶懶的,不過這部分卻也是滿重要的一部分,前一篇介紹 JDBCTemplate ,已經覺得跟以前寫SQL方式有所差異了,JPA帶來的是物件導向的設計面思考,說到JPA不得不提提 ORM ,Object-relational mapping主要想法為簡化及物件導向的設計,讓RDB更貼近Object,在設計上可以更加便利,甚至透過一些設計可以讓Table具有物件導向的特性如繼承等等,以往要使用ORM的框架,都會先以 Hibernate 進行,不過近來慢慢地轉向JPA,主要還是在減少程式碼、增加彈性等等,大體的功能沒有差異很大,所以從Hibernate轉到JPA問題不大,JPA要介紹的東西還滿多的,所以我這裡會再分成三個章節來介紹。 SPRING DATA JPA基本操作 JPQL & Named SQL & Native SQL Cache & DB Design Pattern SPRING DATA JPA更加簡化的程式撰寫,只需要一個 Interface內寫一些查詢 method就可以操作JPA,因為利用 method 組合查詢條件,確實很方便也很容易理解,若是都沒有辦法符合需求當然也可以自己實作一個來用當然沒有問題。 學習目的 :SPRING DATA JPA基本操作。 學習時數 :3.5hr 教學影片: pom.xml 說明 spring-boot-starter-web:配置 Web Project所需的函式庫。 spring-boot-starter-test:配置 unit or mock test 所需的函式庫。 spring-boot-starter-actuator:配置監控spring boot所需的函式庫,後續spring cloud會使用到,所以一開就導入。 spring-boot-starter-jdbc:配置使用jdbc所需的函式庫。 postgresql:配置postgresql連接Driver所需的函式庫。 jasypt-spring-boot-starter:加解密所需的函式庫。 spring-boot-starter-data-jpa:配置Spring data jpa所需的函式庫。 ...