2014年11月21日 星期五

Windows 7 待命記憶體,很肥...

最近碰到一個Windows 7的鳥問題,

windows 7 待命記憶體 佔了超過2G的 RAM的問題,

但是卻沒法關掉這個鬼待命...


後來上網找了很多方法,

僅能減少,無法關閉....

以下是減少待命吃的記憶體大小方法


  1. 到 電源管理,關閉 系統休眠、睡眠。
  2. 若是使用內建顯示卡,請至 BIOS裡,設定 顯示卡記憶體大小,
    不要使用 MAX or AUTO 這類的設定。
  3. 降低 螢幕解析度

以上。

OPENCART 2.0 Bug...

最近因為工作需要,

開始在使用 Opencart 2.0,

用了之後發現官方放出來的有些小BUG,

以下就記錄一些我碰到的問題 與 解法,


  1. 後台管理 Mail 無法寄送
    • 原因:少寫code
    • 修改目標 {projectPaht}/dmin/view/template/marketing/contact.tpl
      line 195 : function send(url) { 下面新增以下內容
    • var sHTML = $("#input-message").code();
      $("#input-message").val(sHTML);
  2. 模組:特價商品、最新商品,新增 佈置後在頁面上卻看不到
    • 新增後,不知在何種原因下,操作者可以預先佈置到頁面上,進而導致它是沒有記錄到模組ID,
    • 所以只需要將 佈置裡的模組先清除掉,再選一次就OK了。
  3. 模組:熱賣商品無法儲存
    • 原因:寫錯code id
    • 修改目標 {projectPaht}/admin/controller/module/bestseller.php
    • line 109: (修改成與我紅字部份一樣即可)
    • if (isset($this->request->post['bestseller_module'])) {
    • foreach ($this->request->post['bestseller_module'] as $key => $value) {
    • if (!$value['width'] || !$value['height']) {
    • $this->error['image'][$key] = $this->language->get('error_image');
    • }
    • }
    • }
  4. SMTP 設定
    • 特別之處在於 SMTP 用戶,請只輸入 用戶ID,不需要完整的Mail



以上, 暫時只有這些發現。

    

2014年7月17日 星期四

MySQL Partition

MySQL Partition


  • 使用 Partition 的優點

    1. 可以有效減少單一檔案存放過多資料而造的檔案肥大問題。
    2. 一些查詢可以得到極大的最佳化
      • 滿足一個給定WHERE 語句的數據可以只保存在一個或多個分區內,這樣在搜尋時就不用搜尋其他剩餘的分區。
    3. 涉及到例如SUM() 和 COUNT()這樣聚合函數的查詢,可以很容易地進行並行處理。
      • 通過「並行」, 這意味著該查詢可以在每個分區上同時進行,最終結果只需通過總計所有分區得到的結果。
    4.  通過跨多個磁盤來分散數據查詢,來獲得更大的查詢吞吐量。
  • 分區類型

    • RANGE 分區:基於屬於一個給定連續區間的列值,把多行分配給分區。
    • KEY 分區:類似於按HASH分區,區別在於KEY分區只支援計算一列或多列,
      且MySQL 伺服器提供其自身的哈希函數。必須有一列或多列包含整數值。
    • HASH分區:基於用戶定義的資料表達式的返回值來進行選擇的分區,
      該資料表達式使用將要插入到資料表中的這些行的列值進行計算。
      這個函數可以包含MySQL 中有效的、產生非負整數值的任何資料表達式。
    • LIST 分區:類似於按RANGE分區,
      區別在於LIST分區是基於列值匹配一個離散值集合中的某個值來進行選擇。

  • 建立方式
    • New table
      • CREATE TABLE employees (
            id INT NOT NULL,
            fname VARCHAR(30),
            lname VARCHAR(30))

        PARTITION BY RANGE (store_id) (
            PARTITION p0 VALUES LESS THAN (6),
            PARTITION p1 VALUES LESS THAN (11),
            PARTITION p2 VALUES LESS THAN (16),
            PARTITION p3 VALUES LESS THAN (21));
    • Existing table
      • alter table employess partition by RANGE (year(remittance_date)) (PARTITION r0 VALUES LESS THAN (2012), PARTITION r1 VALUES LESS THAN (2013), PARTITION r2 VALUES LESS THAN (2014), PARTITION r3 VALUES LESS THAN (2015), PARTITION r4 VALUES LESS THAN (2016), PARTITION r5 VALUES LESS THAN (2017), PARTITION r6 VALUES LESS THAN (2018), PARTITION r7 VALUES LESS THAN (2019), PARTITION r8 VALUES LESS THAN (2020), PARTITION r9 VALUES LESS THAN MAXVALUE );
  • 注意事項
    • 若 index 已經做了, 要先將索引清除,partition完成後, 再重做 index。
    • Partition 時, 用來做為分區的key 必須為 pk 或是 pk 的一部份。
    • 若想做為分區的key不為pk時, 最簡單的做法為把pk先暫時清除掉, 再做 paration, partition 完成後, 再重新加回 pk 。
    • 上述之事項,若沒注意,你將會得到很多的 Error Code: 1503...

2014年6月20日 星期五

Java - Sitemesh 3

由於更換到 JEE 7, 原本的 sitemesh 2.4 也不知道為啥就不 work 了,

你罷工, 那就順便退休吧.


使用 SiteMesh3 最低需求:
  • JDK 1.5
  • A Servlet 2.5 compliant container
  • The SiteMesh runtime library

使用步驟
  1. 到 Sitemesh3 下載 library,放到 專案的 lib 下
  2. 建立最基本範例
  • web.xml 內容新增:
        <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>*</url-pattern>
  </filter-mapping>
  • 新增 sitemesh3.xml 在 {Project}\WebContent\WEB-INF 下
    • Path 起啟路徑為 {Project}\WebContent\,都是路徑 與 sitemesh2  完全不同...
    • decorator 是裝飾目標的位置
<pre class="prettyprint">

<?xml version="1.0" encoding="UTF-8"?>

<sitemesh>

        <!-- 這句代表,不需要被裝飾的 Page -->

<mapping path="/WEB-INF/jsp/admin/login.jsp*" exclue="true"/>

        <!-- 這句代表,只要符合這路徑的 Page ,都會被 Sitemesh 裝飾 -->

<mapping path="/WEB-INF/jsp/admin/*" decorator="/decorators/basic.jsp" />

</sitemesh>

</pre>

  • 依 sitemesh3.xml 裡所設定的 decorator 路徑,新增其內容
    • 新增 decorators 資料夾在 WebContent
    • 新增 basic.jsp 在 {Project}\WebContent\decorators
    • sitemesh3 沒有 taglib,取而代之為
      • <sitemesh:write property="title" />
      • <sitemesh:write property="head"/>
      • <sitemesh:write property="body" />

<html>
  <head>
    <title>SiteMesh example: <sitemesh:write property='title'/></title>
    <sitemesh:write property='head'/>
  </head>
  <body>

    <h1 class='title'>SiteMesh example site: <sitemesh:write property='title'/></h1>


    <div class='mainBody'>

      <sitemesh:write property='body'/>
    </div>

    <div class='disclaimer'>Site disclaimer. This is an example.</div>

  </body>
</html>

參考:Sitemesh3

JEE7 - WebSocket Client

Web Service 可使用的 WS Client, 每次使用前都先做 連線確認.
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

@ClientEndpoint
public class WSClient {
    
    private static final Logger logger = Logger.getLogger("WSClient");

    private static WSClient instance = null;
    
    public static WebSocketContainer container = null;//
    public static Session session = null;
    
    public static String wsServerURL = "ws://localhost:8080/ws";
    
    // flag for connect of websocket
    public static boolean isConnect = false;
    
    public static WSClient createInstance(String wsServerURL) {
        logger.log(Level.INFO, "createInstance");
        WSClient.wsServerURL = wsServerURL;
        
        instance = new WSClient();
        instance.autoConnection(2);
        return instance;
    }
    
    public static void destroyInstance() {
        terminateToServer();
        instance = null;
    }
    
    public static WSClient getInstance() {
        return instance;
    }
    
    public boolean connectToServer() {
        try {
            // Tyrus is plugged via ServiceLoader API. See notes above
            container = ContainerProvider.getWebSocketContainer();

            // WS1 is the context-root of my web.app
            // ratesrv is the path given in the ServerEndPoint annotation on
            // server implementation
            session = container.connectToServer(WSClient.class, URI.create(wsServerURL));
            isConnect = true;
        } catch (Exception e) {
            e.printStackTrace();
            isConnect = false;
        }
        
        return isConnect;
    }
    
    public static void terminateToServer() {
        if (session != null) {
            try {
                session.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            session = null;
            container = null;
            isConnect = false;
        }
    }
    
    public synchronized void autoConnection(int retryCountLimit) {
        // 沒連線的情況下才進去
        if (!isConnect) {
            
            int retryCount = 0;
            do {
                retryCount++;
                connectToServer();
                
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
                
            } while (!isConnect && retryCount < retryCountLimit);
            
        }
    }
    
    public void send(String msg) {
        
        autoConnection(2);
        
        if (isConnect) {
            try {
                session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    @OnOpen
    public void onOpen(Session session) {
        // same as above
        logger.log(Level.INFO, "onOpen" + session.getId());
        isConnect = true;
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        // the new USD rate arrives from the websocket server side.
        logger.log(Level.INFO, "onMessage: " + message);
    }
    
    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        logger.log(Level.INFO, "onClose: " + session.getId());
        
        isConnect = false;
    }
    
}

2014年6月17日 星期二

JEE7 - WebSocket Server Example

The following example shows how to create an endpoint by extending the Endpoint class:

package websocketTest;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/dukeetf")
public class ETFEndpoint {

 private static final Logger logger = Logger.getLogger("ETFEndpoint");

 static Queue<Session> queue = new ConcurrentLinkedQueue<>();

 @OnMessage
 public void onMessage(Session session, String msg) {
  try {
   session.getBasicRemote().sendText(msg);
  } catch (IOException e) {

  }
 }

 /* PriceVolumeBean calls this method to send updates */
 public static void send(double price, int volume) {
  String msg = String.format("%.2f, %d", price, volume);
  try {
   /* Send updates to all open WebSocket sessions */
   for (Session session : queue) {
    session.getBasicRemote().sendText(msg);
    logger.log(Level.INFO, "Sent: {0}", msg);
   }
  } catch (IOException e) {
   logger.log(Level.INFO, e.toString());
  }
 }

 @OnOpen
 public void openConnection(Session session) {
  /* Register this connection in the queue */
  queue.add(session);
  logger.log(Level.INFO, "Connection opened.");
 }

 @OnClose
 public void closedConnection(Session session) {
  /* Remove this connection from the queue */
  queue.remove(session);
  logger.log(Level.INFO, "Connection closed.");
 }

 @OnError
 public void error(Session session, Throwable t) {
  /* Remove this connection from the queue */
  queue.remove(session);
  logger.log(Level.INFO, t.toString());
  logger.log(Level.INFO, "Connection error.");
 }
}

2014年6月16日 星期一

Eclipse 4.3 安裝流程

預防老人痴呆發作,還是先寫下開發環境的安裝流程
  1. 下載、安裝 Java SE,設定Java環境變數
    • JAVA_HOME=C:\Java\jdk1.7.0_51
    • path 新增 %JAVA_HOME%\bin
  2. 下載、解壓縮 Eclipse
  3. 開啟 Eclipse 後,安裝常用插件
    • Git
    • JBoss(WildFly) Tool
    • Spring

終於有官方的 WebSocket

Java 太混了,

WebSocket 到了 JEE 7 才開始支援,

JBoss 也要換到 WildFly 8.0 版也才支援 JEE 7...

一次要換二樣東西,

真麻煩。


想到舊有的 service 都要測試過,

才知道能不能全面更換...


參考:Java EE Documentation、WildFly 8 Final is released!