Skip to main content

JEAP 6 - 動態新增Server 的方法

原先以為只要用 GUI 就夠了,但是有些使用者,因為想要自動化,所以尋問是否有其他的方式。

這裡提供4種方式:

1. 使用GUI 畫面增加
2. 使用 CLI 的command 的方式。
3. 使用 HTTP 的 management API
4. 使用 Java 程式


請先啓動AP Server 的 Domain Mode.

1. 使用GUI 畫面增加








完成後就可以看到 server-four 被建立成功拉!



2. 使用 CLI 的command 的方式。

先到JBoss安裝目錄/bin 下執行jboss-cli.sh (或是 bat ) 啓動 CLI,

聯結後,打入下列指令就可以建立成功拉!

your_host_name  = 想要安裝的host名稱
new_server_name = 新建立的伺服器名稱 
offset_number = port要錯開的數字, 100 的話,所有建立的port 都是預設值+100 
your_server_group = 建立的server所屬的group

/host=your_host_name/server-config=new_server_name:add(auto-start=true,group=your_server_group,socket-binding-port-offset=offset_number


看到 "outcome" => "success" 的回應就成功了。


3. 使用 HTTP 的 management API

server_name =  JBoss 管理的Server IP 或是 hostname
management_port = 預設是9990
auto_start = 新建立的server 是否自動啟動(true or false)

your_host_name  = 想要安裝的host名稱
new_server_name = 新建立的伺服器名稱 
your_server_group = 建立的server所屬的group
offset_number = port要錯開的數字, 100 的話,所有建立的port 都是預設值+100 
id = 登入JBoss管理的 ID 
pwd = 登入JBoss管理的密碼

curl --digest -D - http://server_name:management_port/management --header "Content-Type: application/json" -d '{"operation":"add", "include-runtime":"true" , "address":["host","your_host_name","server-config","new_server_name"],"group":"your_server_group","socket-binding-port-offset":"offset_number","auto-start":"auto_start", "json.pretty":1}' -u id:pwd




看到 "outcome" => "success" 的回應就成功了。

4. 使用 Java 程式

這裡要使用JBoss 提供的API 去呼叫


org.jboss.as.controller.client.ModelControllerClient;
org.jboss.as.controller.client.OperationBuilder;
org.jboss.as.controller.client.helpers.ClientConstants;
org.jboss.dmr.ModelNode;

  Sample 是一個Servlet, 部署上去後,直接呼叫  http://localhost:8680/cliTest/AddUser 就會執行囉~ Sample 程式的重點就是把該帶的參數帶進去而已。

request.get(ClientConstants.OP).set("add");
request.get(ClientConstants.OP_ADDR).add("host", "master");
request.get(ClientConstants.OP_ADDR).add("server-config", "server-six");
request.get("include-runtime").set(true); request.get("group").set("main-server-group");
request.get("socket-binding-port-offset").set("650");
request.get("auto-start").set(true);

package com.test;

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.List;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.RealmCallback;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.OperationBuilder;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.dmr.ModelNode;

/**
 * Servlet implementation class DSStats
 */
@WebServlet("/AddUser")
public class AddUser extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  try {

            PrintWriter out = response.getWriter();
            List results = addUser();
            for (ModelNode result : results) {
                out.println("result: " + result.asString());
            }

            out.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static List addUser() throws IOException {
        final ModelNode request = new ModelNode();

        request.get(ClientConstants.OP).set("add");
        request.get(ClientConstants.OP_ADDR).add("host", "master");
        request.get(ClientConstants.OP_ADDR).add("server-config", "server-six");
        request.get("include-runtime").set(true);
        request.get("group").set("main-server-group");
        request.get("socket-binding-port-offset").set("650");
        request.get("auto-start").set(true);
        
        ModelControllerClient client = null;
        try {
         
            client =createClient(InetAddress.getByName("localhost"), 10999,"admin","Christina7979!".toCharArray(),"ManagementRealm");
            
            final ModelNode response = client.execute(new OperationBuilder(request).build());

            return response.asList();
        } finally {
            safeClose(client);
        }
    }

    public static void safeClose(final Closeable closeable) {
        if (closeable != null) try {
            closeable.close();
        } catch (Exception e) {
            // no-op
        }
    }
    
    
    private static ModelControllerClient createClient(final InetAddress host, final int port, final String username, final char[] password, final String securityRealmName) {

     final CallbackHandler callbackHandler = new CallbackHandler() {

      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
       
       for (Callback current : callbacks) {
        System.out.println("current callbacks:["+current+"]");
            if (current instanceof NameCallback) {
             System.out.println("username:["+username+"]");
                NameCallback ncb = (NameCallback) current;
                ncb.setName(username);
            } else if (current instanceof PasswordCallback) {
                PasswordCallback pcb = (PasswordCallback) current;
                pcb.setPassword(password);
                System.out.println("password:["+new String(pcb.getPassword())+"]");
            } else if (current instanceof RealmCallback) {
                RealmCallback rcb = (RealmCallback) current;
                rcb.setText(rcb.getDefaultText());
                System.out.println("RealmCallback:["+rcb.getDefaultText()+"]");
            } else {
                throw new UnsupportedCallbackException(current);
            }
       }
      }
     };

     return ModelControllerClient.Factory.create(host, port, callbackHandler);
    }

}



看到 "outcome" => "success" 的回應就成功了。


以上

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 …