Skip to main content

JEAP 6 - HornetQ 的 記事(Journal) 原理說明

有些人問HornetQ 的是不是可以使用Database 當作它的永久儲存,
其實這有點畫蛇添足,因為以現今的技術,資料庫的存取其實是屬於烏龜一族的,
HornetQ 使用更先進的方式,不再去連接慢吞吞有耗資源的資料庫,

這裡要先說明一下 HornetQ 的原理,

HornetQ 是利用自己的記事(Journal)檔案去記錄,靠使用 Linux AIO 或是 Java NIO. 
從官網的部落格看到是用 Linux libaio,直接用Linux 核心的 DMA, 直接寫入 DMA (Direct Memory Access) Buffer ,而不是傳統的CPU Copy,也就是說不透過CPU 獨立地直接快速讀寫系統記憶體。然後儲存在檔案上。有關 Linux AIO IBM 有個文章寫得非常好 ,大家可以進去看看,因為我還是喜歡以 Java 與 J老板為主,因此就不多做說明了。

HornetQ 每次都會把送進來的訊息儲存在記事(Journal)檔案裡面,記事(Journal)檔案是許多事先配給的檔案。每個檔案的大小都可以剛好放在硬碟磁柱上。(大多時候都是 10 MiB, 但是可能會因為你使用的系統而有些不同)。為了要減少磁碟的控制,浪費效能,只能往這些記事(Journal)檔案加入內容,而且永遠只加在現在使用的檔案上。

就算刪除也只是在檔案的最後加上一個刪除的紀錄,系統會看這些刪除的紀錄,如果檔案裡面的資料都被記錄刪除了,這個檔案就可以被重複使用了。 這些記事(Journal)檔案也可以支援交易,要所有的交易內容都完成寫在disk上了,它才會認為commit. 

而讀取這些檔案的方式有兩種: NIO 與 AIO

NIO 是一個Java 的快速存取方式,盡量避免磁碟的移動。如果你用的系統不是 Linux 或是 Libaio, HornetQ 內間就是用這個純 Java 的方法拉!

AIO 可以透過 JNI 與 Linux對話。直接將訊息的bytecode透過libaio 的 aio_write 寫到磁碟裡面,然後就等待Call back回傳。 而且寫這些訊息都是開多條平行執行的Thread (執行緒) 去寫,以前每寫一個進磁碟都要等待sync候才能繼續,這樣太慢了。用平行與 call back 的方式才可以完全利用系統的效能到極限。



至於檔案的極限,只要你的硬碟夠大,HornetQ都可以支援,目前測試過,是在 8 GiB 而且Server 跟 Client 本身只有 50MiB 的 RAM!

講了這麼多,到底在哪裡可以設定JEAP 6 裡面的HornetQ 去用AIO 還是 NIO 呢?
設定messaging 的 subsystem,  請到 standalone or domain.xml , 找到你正在使用的 messaging profile, 加入  <journal-type>你要的方式</journal-type>

            
                true
                true
                102400
                2
                NIO   
           
           


這樣就可以拉!

對了,有些人會問我說,為什麼我照著設定了,可是每次Server重開以後,我的訊息就會不見,根本沒有存到磁碟裡!我常常很囧的發現,JMS 的程式裡面居然沒有去設定這個訊息要保留,當然就沒有囉!!! 記得加喔!

publisher.setDeliveryMode(DeliveryMode.PERSISTENT);

package redhat.jmsclient;


import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class MsgSender {
 // Set up all the default values
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/sampleQ";
    private static final String DEFAULT_USERNAME = "xxx";
    private static final String DEFAULT_PASSWORD = "xxx";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://127.0.0.1:4447";
    
    private Context ic = null;
    private ConnectionFactory cf = null;
    private Connection connection = null;
    public String msg = "Hello Christina at ";
    
    public static void main(String[] args) throws Exception {

     MsgSender sender = new MsgSender();
     if(args != null && args.length>=1 &&args[0] != null){
      sender.msg = args[0];
      }
     sender.send();

 }
    
    public MsgSender(){
     
     
    }

 public void send(){

     final Hashtable env = new Hashtable();
     env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
     env.put(Context.PROVIDER_URL, PROVIDER_URL);
     
    
     
     try {         
      ic = new InitialContext(env);
      cf = (ConnectionFactory)ic.lookup(DEFAULT_CONNECTION_FACTORY);
      Queue queue = (Queue)ic.lookup(DEFAULT_DESTINATION);

      connection = cf.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer publisher = session.createProducer(queue);
  
      connection.start();
      msg = "Hello Christina at "+System.currentTimeMillis();
      TextMessage message = session.createTextMessage(msg);
      publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
      publisher.send(message);

      System.out.println("Message:["+msg+"] sento to the JMS Provider");

     }
      catch (Exception exc) {
        exc.printStackTrace();
      }
     finally {         
       if (connection != null)   {
         try {
            connection.close();
         } catch (JMSException e) {                    
           e.printStackTrace();
         }
     } 
  
 }
 }
}

Comments

Popular posts from this blog

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

RHTE - Supercharge your integration services

Red Hat Tech Exchange has taken place in Vietnam, Ho Chi Minh city two weeks ago, it is a great event held by Red Hat in Asia Pacific Region. It is open to all Red Hat partners who are interested in learning what Red Hat is doing recently, see what the trend of the open source world, basically it is a great event to share your knowledge and experience, to meet other enthusiastic people.

I am very fortunate to talk in this great event, to talk about the things I have been working on and even discuss it with many. Also got lots of great ideas too. So here are the slide.

My first talk was with Thomas Qvarnström about how to handle large size data in JBoss Fuse and how JBoss Data Grid can help in the situation.

Here is the agenda of the talk, we will be talk about this in the up coming webinar on 24th Sept.

Integration often involves storing, retrieving, and transforming data. Using a traditional database in your integration is likely to becomes a bottleneck that is expensive and hard to …

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …