2020年11月22日 星期日

無法透過XPath處理的XML字串

 要處理的XML字串如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans><bean id="AB01"><property name="output"><map><entry><key><value>ID</value></key><value>01</value></entry><entry><key><value>ACNO</value></key><value></value></entry><entry><key><value>AMT</value></key><value></value></entry></map></property></bean></beans>


因為無法使用以下這種javax.xml.xpath的方式處理
XPathExpression expression = xPath.compile("//beans//bean//property[@name='output']");

所以改用org.w3c.dom的方式處理
Element eElement = (Element) nNode;
eElement.getElementsByTagName("entry").item(0).getTextContent());
會抓出ID01,再自己substring處理
雖然可以達到目的,但感覺應該有更好的方式

經網友指點,亦可使用以下方式處理eElement.getElementsByTagName("entry").item(0).getChildNodes().item(0).getTextContent()
=>可以取得ID

eElement.getElementsByTagName("entry").item(0).getChildNodes().item(1).getTextContent()
=>可以取得01

應該也可以使用JAXB,但是沒用過JAXB,不熟
日後再研究

Servlet輸出中文有問號???

已經確認資料來源編碼沒問題,log也可以正常顯示中文字
但Servlet頁面一直顯示問號???

解決方式:
需在response.getWriter()的之前加入以下設定
response.setContentType("text/html;charset=UTF-8");
如果使用Big5加入
response.setContentType("text/html;charset=Big5");

xml來源是Big5編碼的處理方式

InputStream xmlInputStream = new ByteArrayInputStream(resXmlString.getBytes("Big5"));

DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();

InputSource is = new InputSource(xmlInputStream);

is.setEncoding("Big5");  

Document xmldoc = xmlBuilder.parse(is);


備註:
不設定setEncoding會遇到的錯誤訊息是
英文:Invalid byte 1 of 1-byte UTF-8 sequence
中文:1-byte UTF-8 序列的無效位元組1

2020年11月18日 星期三

-Dfile.encoding 指定 JVM 預設編碼

 https://openhome.cc/Gossip/Encoding/JVMEncoding.html

有些 API 若不指定編碼,通常會使用 JVM 預設編碼,預設會與作業系統預設編碼相同,像是 String 建構式、 getBytes() 方法或這邊看到的 FileReader 等(其他還有 java.iojava.utiljava.net 等套件中的一些 API),可以使用 Charset.defaultCharset() 取得預設編碼。

在啟動 JVM 時,其實可以使用 -Dfile.encoding 指定 JVM 預設編碼,例如:

C:\workspace>java -Dfile.encoding=UTF-8 cc.openhome.Main

getBytes() encodes a String into a byte array using the platform's default charset
https://www.baeldung.com/string/get-bytes

https://openhome.cc/Gossip/Encoding/String.html

ref:
https://openhome.cc/Gossip/Encoding/index.html

https://www.javaworld.com.tw/jute/post/view?bid=21&id=282136&tpg=1&ppg=1&sty=1&age=0#282136

https://stackoverflow.com/questions/12659417/why-does-javas-string-getbytes-uses-iso-8859-1

https://docs.oracle.com/javase/tutorial/i18n/text/string.html

2020年11月4日 星期三

XML XPathExpression

https://stackoverflow.com/questions/15421847/retrieving-attributes-and-values-of-child-nodes-of-an-xml-element-in-java

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;


    final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    final XPath xPath = XPathFactory.newInstance().newXPath();

    final XPathExpression expression = xPath.compile("//course[@name='AdvancedAlgorithm']//Teacher");


    final NodeList nodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);


    for (int i = 0; i < nodeList.getLength(); ++i) {

        System.out.println(((Element)nodeList.item(i)).getAttribute("name"));

    }


2020年8月24日 星期一

JavaModelGeneratorConfiguration

可以用在Database Reverse Engineer

http://mybatis.org/generator/apidocs/org/mybatis/generator/config/JavaModelGeneratorConfiguration.html

package org.mybatis.generator.config;
public class JavaModelGeneratorConfiguration extends PropertyHolder {


ref:
https://github.com/lihengming/spring-boot-api-project-seed/blob/master/src/test/java/CodeGenerator.java

Four Ways of MyBatis_Generator (MBG) Reverse Engineering  
https://programmer.ink/think/four-ways-of-mybatis_generator-mbg-reverse-engineering.html

fieldItem_index = 0

因為通常pk會放在第一個欄位
可以用這種比較笨的方式判斷

    <#if fieldItem_index = 0>
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    </#if>


ref:
https://freemarker.apache.org/docs/ref_directive_list.html#ref_list_accessing_state

Invalid object name 'hibernate_sequence'

https://stackoverflow.com/questions/49037803/sql-server-2016-invalid-object-name-hibernate-sequence

Avoid GenerationType.AUTO

Set it explicit to GenerationType.IDENTITY or GenerationType.SEQUENCE depending on what you want or your DB supports.

中文訊息:
com.microsoft.sqlserver.jdbc.SQLServerException: 無效的物件名稱 'hibernate_sequence'

2020年8月20日 星期四

SpringBootCodeGenerator

 https://github.com/moshowgame/SpringBootCodeGenerator

 

基于SpringBoot2+Freemarker的代码生成器,

√支持mysql/oracle/pgsql三大数据库, 

√用DDL-SQL语句生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL相关代码

 

ref:

https://zhengkai.blog.csdn.net

  

2020年8月5日 星期三

@Transactional(rollbackFor = Exception.class)

因為Spring的設計之初參考EJB,預設只會對unchecked exceptions去rollback,如果自己開發的程式對自訂Business Exception(比如傳入的訂單金額為負數)需要RollBack,需要用annotation方式設定@Transactional(rollbackFor = Exception.class),對checked exceptions也rollback。


Although EJB container default behavior automatically rolls back the transaction on a system exception (usually a runtime exception), EJB CMT does not roll back the transaction automatically on anapplication exception (that is, a checked exception other than java.rmi.RemoteException). While the Spring default behavior for declarative transaction management follows EJB convention (roll back is automatic only on unchecked exceptions), it is often useful to customize this behavior.


ref:

2020年7月26日 星期日

off-heap memory方式處理大量資料

如果真的有需要在JVM操作大量資料,又怕heap設定太大造成GC會STW(Stop the World),另一種做法是使用off-heap memory方式,把資料處理拉到OS層級,節省了記憶體的代價就是犧牲速度。

Off Heap Cache - OHCache - Simple Example
https://www.isaacnote.com/2018/09/ohcache-simple-example.html 

OHC - An off-heap-cache
https://github.com/snazy/ohc
OHCacheBuilder
https://github.com/snazy/ohc/blob/master/ohc-core/src/main/java/org/caffinitas/ohc/OHCacheBuilder.java
public OHCache<K, V> build()
{
    if (fixedKeySize > 0 || fixedValueSize > 0|| chunkSize > 0)
        return new OHCacheChunkedImpl<>(this); 
    return new OHCacheLinkedImpl<>(this); 
}


使用堆外内存优化JVM GC问题小记
https://juejin.im/post/5cdf8df4f265da1bd260bae9 
Java缓存类型
  • 堆缓存:使用java堆内存来存储对象,好处是不需要序列化/反序列化,速度快,缺点是受GC影响。可以使用Guava Cache、Ehcache 3.x、MapDB实现。
  • 堆外缓存:缓存数据存储在堆外,突破了JVM的枷锁,读取数据时需要序列化/反序列化,比对堆内缓存慢很多。可以使用Ehcache 3.x、MapDB实现。
  • 磁盘缓存:在JVM重启时数据还在,而堆缓存/堆外缓存数据会丢失,需要重新加载。可以使用Ehcache 3.x、MapDB实现。
  • 分布式缓存:没啥好说的了,Redis…

作者:xinlmain
链接:https://juejin.im/post/5cdf8df4f265da1bd260bae9
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Java 大内存应用(10G 以上)会不会出现严重的停顿?
https://www.zhihu.com/question/35109537

如果真的需要使用on-heap方式處理,可以參考HBase或Cassandra等的做法:
Tuning G1GC For Your HBase Cluster
https://blogs.apache.org/hbase/entry/tuning_g1gc_for_your_hbaseCDH6.3 
HBase: G1 GC Tuning with JDK11
https://blog.cloudera.com/cdh6-3-hbase-g1-gc-tuning-with-jdk11/ 
JVM 与 Hbase
https://www.selinux.tech/java/core/jvm-hbase 
Cassandra 3.0 Tuning Java resources
https://docs.datastax.com/en/cassandra-oss/3.0/cassandra/operations/opsTuneJVM.html

2020年7月5日 星期日

sendStringParametersAsUnicode

JDBC預設是true,會自動把字元資料轉成Unicode格式送到伺服器
但是資料庫column不是開nchar之類的Unicode UCS-2欄位
因為SQL Server需要做型別轉換,會需要用到平行處理,會很耗cpu
從執行計畫可以看到"type conversion in expression"的警告訊息
解法一:column改成nvarvhar nchar這類的欄位
解法二:sendStringParametersAsUnicode設定成false,使用非Unicode格式送到伺服器(比如ASCII/MBCS)
今天上許致學老師的課才知道有這參數


"type conversion in expression" sql server

2020年6月23日 星期二

mall项目是一套电商系统,基于SpringBoot+MyBatis实现

後台
https://github.com/macrozheng/mall
http://localhost:8080

前台(Vue+Element)
https://github.com/macrozheng/mall-admin-web
前台佈署
http://www.macrozheng.com/#/deploy/mall_deploy_web
npm install node@8.9.4
修改\config\dev.env.js,把BASE_API改成自己開發環境網址(預設是8080)
npm run dev
登入頁面
http://localhost:8090
預設帳密:admin/macro123

微服務
https://github.com/macrozheng/mall-swarm

教學
https://github.com/macrozheng/mall-learning

※Docker佈署
 http://www.macrozheng.com/#/deploy/mall_deploy_docker

請先修改Docker組態檔開放TCP 2375 (有資安風險,請僅用於開發環境!)
vi /usr/lib/systemd/system/docker.service
加入以下組態
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
參考
https://www.cnblogs.com/hongdada/p/11512901.html

重啟Docker服務
systemctl daemon-reload
systemctl restart docker

\mall\pom.xml內的<docker.host>修改成自己的Docker主機IP位址
\mall\mall-admin\pom.xml內的<plugin>區塊請取消註解,並package推送images
\mall\mall-search\pom.xml內的<plugin>區塊請取消註解,並package推送images
\mall\mall-portal\pom.xml內的<plugin>區塊請取消註解,並package推送images


package推送images:



package推送images後使用docker images指令確認推送成功到伺服器:


# 啟動所有的 containers
因為啟動有順序性,第一次啟動失敗再執行一次指令
$ docker start $(docker ps -a -q)
參考
https://ithelp.ithome.com.tw/articles/10186431


※編譯問題
因為mall-admin專案相依於mall-mbg和mall-security,又間接相依於mall-common,請依下圖方式先mvn install把相依專案編譯成jar安裝到local repository

編譯錯誤訊息
[ERROR] Failed to execute goal on project mall-mbg: Could not resolve dependencies for project com.macro.mall:mall-mbg:jar:1.0-SNAPSHOT: Could not find artifact com.macro.mall:mall-common:jar:1.0-SNAPSHOT -> [Help 1]


※Lombok
以下package的程式有使用到Lombok
package com.macro.mall.dto;
package com.macro.mall.portal.domain;
package com.macro.mall.security.config;

2020年3月14日 星期六

build.gradle exclude "META-INF/*.RSA"

https://stackoverflow.com/questions/49519467/execute-jar-with-included-dependencies-using-gradle

task BuildJar(type: Jar) {
    manifest {
        attributes(
                'Main-Class': 'org.mycompany.mainclass'             
        )
    }
    classifier = 'all'
    baseName = 'MyJar'
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
         }
            {
                exclude "META-INF/*.SF"
                exclude "META-INF/*.DSA"
                exclude "META-INF/*.RSA"
            }
    with jar

}