2020年12月18日 星期五
JCConf Taiwan 2020影片
官網: https://jcconf.tw/2020/
共筆: https://hackmd.io/@JCConf/2020/
2020年12月5日 星期六
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(
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInst
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.io、java.util、java.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
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年10月23日 星期五
Bauke Scholtz先生整理Java core libraries用到的Design Patterns
Bauke Scholtz先生整理Java core libraries用到的Design Patterns,滿詳細的https://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries
Bauke Scholtz先生:
https://stackoverflow.com/users/157882/balusc
另一個討論串:
https://stackoverflow.com/questions/3068912/what-is-the-most-used-pattern-in-java-io
2020年8月25日 星期二
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月22日 星期六
output ${expression} in Freemarker
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月18日 星期二
2020年8月5日 星期三
@Transactional(rollbackFor = Exception.class)
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.2020年7月26日 星期日
off-heap memory方式處理大量資料
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堆内存来存储对象,好处是不需要序列化/反序列化,速度快,缺点是受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
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/
※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後使用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年6月14日 星期日
Jelastic PaaS建立Auto-Scalable TomEE Cluster
Java Cloud Hosting: Elasticity and Flexibility in a Turnkey PaaS
https://jelastic.com/blog/java-hosting-paas/
Dev Panel Guide
https://docs.jelastic.com/dashboard-guide
Ops Panel Guide
http://ops-docs.jelastic.com/introduction
Java Garbage Collection Types and Settings in Jelastic PaaS
https://jelastic.com/blog/garbage-collection/
https://stackoverflow.com/questions/tagged/jelastic
2020年6月12日 星期五
重试机制!java retry(重试) spring retry, guava retrying 详解
備用URL: https://mp.weixin.qq.com/s/yhYpbVd3pYi4DByRAlfCgQ
spring-retry 和 guava-retry 工具都是线程安全的重试,能够支持并发业务场景的重试逻辑正确性。
spring-retry:
org.springframework.retry.support.RetryTemplate
https://github.com/rholder/guava-retrying
https://github.com/houbb/sisyphus
支持 fluent 过程式编程
无缝接入 spring
解决 spring-retry 与 guava-retrying 中的不足之处
2020年5月23日 星期六
libphonenumber
DEMO:
https://libphonenumber.appspot.com
ref:
https://www.twilio.com/blog/validating-phone-numbers-effectively-with-c-and-the-net-frameworks
https://medium.com/frochu/libphonenumber-example-app-f60680faa599
2020年3月14日 星期六
build.gradle exclude "META-INF/*.RSA"
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
} 

