Skip to main content

JEAP 6 - HornetQ Cluster

又是應觀眾要求。好像已經被問了一兩次了,
我其實本來還想要PO 多一點BRMS上來的說,但是再不放上來又要被問N次了..
其實非常簡單,既然是這樣,就花點時間PO上來吧。
================================================

這篇再看這邊之前,請先

A. 學會設定JBoss Cluster (Standalone or Domain), 為了快速與方便,本次使用Standalone Cluster, domain 設定也是一樣的,所以就不再贅述。(連結)
B. 學會設定 HornetQ 的ConnectionFactory 與 Queue。(連結)

先前的JBoss 除了把JBoss Messaging 設定起來後,若要呼叫,就要另外設定HA-JNDI, 新的JBoss EAP 6 已經不需要JNDI 了,只要使用Remote就好了,它自動就會去分配到不同的機器上。


這次是把所有的JBoss 與它的HornetQ 的Cluster 設定起來,之後使用JMS 的單獨的Client 努力的發送訊息(message) 到 Queue 中。然後再寫個Receiver 去接收 Cluster 每一個node queue中的訊息。


1. 先將JBoss 設定為Cluster ,請確認使用 standalone-full-ha.xml ,domain的話請使用 full-ha 的 profile。然後就可以進入正題,請找到HornetQ 設定的subsystem.
先把 Clustered 設定為true, 與互相溝通的 user name與password

   
      true
      clusteruser 
      cluster-secret
      
   


2. 開始設定上面的ConnectionFactory, 這裡我是使用裡面default已經設好的jms/RemoteConnectionFactory。



   
   
   
      
         
      
      
         
      
      true 
      3 
    
    
    


3. 到JBoss Admin console上加上這次要使用的queue,與建立Application Server (請參考連結)

a.建立Queue

b.建立使用者
Lin-Christinateki-MacBook-Air:bin weimeilin$ ./add-user.sh

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties) 
(a): (告訴系統是AP在用的)

Enter the details of the new user to add.
Realm (ApplicationRealm) : 
Username : christina (使用者名稱)
Password : (輸入密碼)
Re-enter Password : (輸入密碼)
What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: remoteJMS (使用者的角色,在這裡我自定為remoteJMS)
About to add user 'christina' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'christina' to file '/Applications/demo/jboss-eap-6.0/standalone/configuration/application-users.properties'
Added user 'christina' to file '/Applications/demo/jboss-eap-6.0/domain/configuration/application-users.properties'
Added user 'christina' with roles remoteJMS to file '/Applications/demo/jboss-eap-6.0/standalone/configuration/application-roles.properties'
Added user 'christina' with roles remoteJMS to file '/Applications/demo/jboss-eap-6.0/domain/configuration/application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller?
yes/no? no

b.建立安全性設定





4. 啟動兩個JBoss,
./standalone.sh -c=standalone-full-ha.xml -Djboss.node.name=node1 -b IP_ADDRESS
./standalone.sh -c=standalone-full-ha.xml -Djboss.node.name=node2 -b IP_ADDRESS




5. 寫一個Producer, 會非常快速的把資料放到queue裡面,

若你要Remote Cluster的話,請在URL 的地方放入所有的Server 的位置。
package redhat.jmsclient;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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 = "christina";
    private static final String DEFAULT_PASSWORD = "christina79";
    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,remote://127.0.0.1:4547";
    
    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.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();
         }
     } 
  
 }
 }
}




發送快速發送個10000次後,

可以看見, 兩個Server上都有很多的訊息,JBoss 會自動幫你平分兩邊的負載,達到負載平衡(Load Balance)



6. 寫個 Consumer ,去抓上面所有的訊息。

package redhat.jmsclient;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
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 MsgReceiver implements MessageListener {
 
 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/sampleQ";
    private static final String DEFAULT_USERNAME = "christina";
    private static final String DEFAULT_PASSWORD = "christina79";
    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,remote://127.0.0.1:4547";

 private ConnectionFactory connectionFactory;

 private Queue queue;
 private Context ic;
 

 public static void main(String[] args) throws Exception {

  MsgReceiver reciever = new MsgReceiver();
  
  
  reciever.consum();

  while (true) {
   Thread.sleep(1000);
  }

 }

 public MsgReceiver() throws Exception {
  final Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
   env.put(Context.PROVIDER_URL, PROVIDER_URL);
  
   
   ic = new InitialContext(env);
   connectionFactory = (ConnectionFactory)ic.lookup(DEFAULT_CONNECTION_FACTORY);
      queue = (Queue)ic.lookup(DEFAULT_DESTINATION);
 }
 
 
 

 private void consum() throws Exception {
  final Connection connection;;

  
  connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
  
  connection.setExceptionListener(new ExceptionListener() {
   
   @Override
   public void onException(JMSException exception) {
    exception.printStackTrace();
    
   }
  });
  
  final Session session = connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);

  final MessageConsumer consumer = session.createConsumer(queue);

  consumer.setMessageListener(this);

  connection.start();

  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    try {
     consumer.close();
     session.close();
     connection.close();
    } catch (JMSException e) {
     e.printStackTrace();
    }
   }
  });
 }

 @Override
 public void onMessage(Message message) {

  if (message instanceof TextMessage) {
   try {
    System.out.println("Received: "
      + ((TextMessage) message).getText());
   } catch (JMSException e) {
    e.printStackTrace();
   }
  } else {
   System.out.println("Received: " + message);
  }
 }

}




以上。超簡單的吧!


Comments

Unknown said…
想問一下JBoss clustering 跟 hornetq cluster 有沒有關係。可以只用hornetq cluster 嗎?
Unknown said…
This comment has been removed by the author.

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 …