Skip to main content

JEAP 6 - 從Server 上Remote 呼叫其他Server 上的 Clustered EJB

老公前幾天才跟我發飆說,如果我晚上回家再不理他,在家裡寫Blog的話
他就要跟我離婚,白天的工作就很忙了,
為了家庭的和睦,所以最近更新會慢一點。。。
=======================================

EJB Server 連結需要在兩邊的Server 做一些基本認證的連接點,
我們把EJB 設定為Cluster, 所以需要有HA 的機制,不會設定的,請到前面幾篇去看(連結)。
再啓動另一台JBoss 去呼叫兩個Cluster 的EJB instance.


呼叫的那個WAR (或是EAR) 請放在我們稱作Client端,被呼叫的EJB Server 我們叫接收端。

接收端
基本上接收端的機器不需要做特別的設定,僅需要建立一個Application user 即可。 所以,請到JBoss_INSTALL_PATH/bin 下,執行 add-user.sh or add-user.bat 如下
(如果你是Domain 的話,僅需要在Domain controller 下執行,如果你是在Standalone的話,請每個Server 都要執行喔!)

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 : christinaejb (使用者名稱)
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)[  ]: (使用者的角色,在這裡直接空白)
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? yes
To represent the user add the following to the server-identities definition <secret value="Y2hyaXN0aW5hZWpiNzk=" /> (等一下要用到的)


請注意最後這個加密後的值。

Client端

這裡才是需要注意的地方,因為這裡我們要在Server上設定可以對外聯接EJB的Connector, 沒錯,就是在Server上開個洞去連結EJB, 因為是開個連結,所以想當然爾就要一些安全性上的設定拉!請到 standalone-full.xml  or standalone-full-ha.xml 或是 domain.xml 裡面的 full or full-ha 的Profile下設定

1. 設定連接的密碼,在下面紅色的地方,打入你剛剛加密後得到的密碼


<management>
        <security-realms>
            <!--...-->
            <security-realm name="ejb-security-realm">
                <server-identities>
                    <secret value="dsHJfKDhhsjdas=="/>
                </server-identities>
            </security-realm>
        </security-realms>
</management>


2. 設定對外聯接的IP 與Socket 位置,也就是你的EJB Server 對外的remote IP 與port 通常default 都是 4447


<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
        <!--...-->
        <outbound-socket-binding name="remote-ejb">
            <remote-destination host="IP_ADDRESS" port="4447"/>
        </outbound-socket-binding>
        <outbound-socket-binding name="remote-ejb1">
            <remote-destination host="IP_ADDRESS" port="4547"/>
        </outbound-socket-binding>
</socket-binding-group>



3. 到Remote subsystem 下面設定你對外的連結名稱

<subsystem xmlns="urn:jboss:domain:remoting:1.1">
  <!--....-->
  <outbound-connections>
     <remote-outbound-connection name="remote-ejb-connection" outbound-socket-binding-ref="remote-ejb" security-realm="ejb-security-realm" username="christinaejb">
<properties>
  <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
  <property name="SSL_ENABLED" value="false"/>
</properties>
      </remote-outbound-connection>

      <remote-outbound-connection name="remote-ejb-connection1" outbound-socket-binding-ref="remote-ejb1" security-realm="ejb-security-realm" username="christinaejb">
<properties>
  <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
  <property name="SSL_ENABLED" value="false"/>
</properties>
      </remote-outbound-connection>
   </outbound-connections>
</subsystem>

其中,
1. remote-ejb-connection 就是你給這個remote connection的名字
2. remote-ejb 剛剛我們設定對外對應的socket名稱
3. ejb-security-realm 剛剛設定安全密碼的名稱
4. christinaejb 建立在ejb Server上的使用者名字
又因為這次是兩個server, 所以分別建立的兩次!

這樣Server上的設定都完成了, 接下來就是要到程式裡面去設定,告訴你的WAR 或是 EAR 你需要用哪幾個remote的 EJB connection

建立一個  jboss-ejb-client.xml , 如果是EAR, 請放在 EAR/META-INF, 如果是WAR 請放在WAR/META-INF

內容如下:


<jboss-ejb-client xmlns="urn:jboss:ejb-client:1.0">
    <client-context>
        <ejb-receivers>
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection"/>
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection1"/>
        </ejb-receivers>
    </client-context>
</jboss-ejb-client>

這裡很容易看出來,他其實就是告訴EAR/WAR 你有哪幾個remote ejb connection 可以用。

以上所有的設定都完成了,寫在JSP or Servlet中的code 也沒什麼大的驚喜,與EJB Client相同。

protected void doInvoke(){
   Context context=null;
   try{
      final Hashtable jndiProperties = new Hashtable();
      jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
      context = new InitialContext(jndiProperties);
   }catch (Exception e) {
      e.printStackTrace();
   }

   MyStatusBeanRemote remote;
   try {
      remote = (MyStatusBeanRemote)context.lookup("ejb:MyEJBProjectEAR/MyEJBProject//MyStatusBean!redhat.remotebeans.MyStatusBeanRemote?stateful");
      System.out.println("1."+remote.getState());
      remote.updateState("Christina");
      System.out.println("2."+remote.getState());
   } catch (NamingException e) {
      e.printStackTrace();
   }
}
之後啓動Server, 可以看到Server 去連結兩個ejb的client. (有個lookup 的小bug,目前我還在找patch中,會出error 但是基本上不會影響工作。AS7-4755  )

執行Servelt之後,可以看到回應很正常!以上!






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 …

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 …

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 …