忍者ブログ

Noracorn Try And Error Programサバサバと働くITエンジニア。忘れやすい頭からサラサラと流れ落ちる情報を、書き留めるブログです。

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

JBOSS5.1 MBean作成

MBeanの作成
 
参考ページ
http://community.jboss.org/wiki/ExampleHelloWorldService
 
作成注意点
インターフェースの名前が、MBeanの名前がSimple.javaだった場合、
必ずインターフェースはSimpleMbean.javaとすること。
MBean → XXX
MBeanインターフェース → XXXMBean
 
MBeanインターフェースクラス
 
package hoge;
 
import org.jboss.system.ServiceMBean;
 
public interface HelloWorldServiceMBean extends ServiceMBean{
String getMessage();
void setMessage(String message);
void printMessage();
}
 
MBeanクラス
 
package hoge;
 
import org.jboss.system.ServiceMBeanSupport;
 
public class HelloWorldService extends ServiceMBeanSupport
implements HelloWorldServiceMBean{
 
private String message = "It is initial Message!!!!";
@Override
public String getMessage() {
return this.message;
}
 
@Override
public void setMessage(String message) {
this.message = message;
}
 
@Override
public void printMessage() {
log.info(message);
}
protected void startService() throws Exception{
log.info("Starting with message : " + this.message);
}
 
protected void stopService() throws Exception{
log.info("Stopping with message : " + this.message);
}
}
 
META-INF/jboss-service.xml
 
<?xml version="1.0" encoding="UTF-8"?>
 
<server>
 <mbean code="hoge.HelloWorldService" name="hoge:service=HelloWorld">
   <attribute name="Message">Hello World!!!!!!!!!!!!!!!!!!!!!!</attribute>
 </mbean>
</server>
 
デプロイ方法
 
%JBOSS_HOME%\server\default\deployに、作成したXXX.sarを入れる。
sarは、タダのjarを名前変更したものです。
sarは、展開されたフォルダの状態で置いてもいいです。
 
確認方法
 
http://localhost:8080/jmx-consoleで確認する。
hogeパッケージに、HelloWorldサービスが出来上がっているはず。
 

拍手[0回]

PR

JBOSS5.1 JMSセキュリティ

 JBOSS5.1
JMS
セキュリティ

JMSのセキュリティ
 
問題
セキュリティ設定されているトピックで、
durableSubscriberを認証されていないユーザで作成使用とするとExceptionが出る。
 
対処1
動的トピック作成をする。
 
JBOSSのMBeanを使用して、動的にトピック作成をする。
そうすると、セキュリティ設定の無いトピックができる。
 
        InitialContext context = new InitialContext();
        MBeanServerConnection mBeanServer
            = (MBeanServerConnection)context.lookup("jmx/invoker/RMIAdaptor");
        ObjectName serverObjectName = new ObjectName("jboss.messaging:service=ServerPeer");
 
        String topicName = "newTopic";
 
        mBeanServer.invoke(serverObjectName, "deployTopic",
                new Object[] {topicName, "topic/" + topicName},
                new String[] {"java.lang.String", "java.lang.String"});
 
対処2
セキュリティ設定をしないで、トピックを作成する。
 
JBOSSに配置するトピック設定ファイルから、セキュリティ設定を削除する。
 
%JBOSS_HOME%\server\XXX\deploy\messaging\トピック設定ファイル.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<!--
    Messaging Example Destinations
-->
 
<server>
 
  <mbean code="org.jboss.jms.server.destination.TopicService"
     name="jboss.messaging.destination:service=Topic,name=testTopic"
     xmbean-dd="xmdesc/Topic-xmbean.xml">
     <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
     <depends>jboss.messaging:service=PostOffice</depends>
     <attribute name="SecurityConfig"> ←このSecurityConfigを削除する。
        <security>
           <role name="publisher" read="true" write="true" create="false"/>
           <role name="noacc" read="false" write="false" create="false"/>
        </security>
     </attribute>
  </mbean>
 
</server>
 
対処3
JMSセキュリティの認証ユーザを作成する。
 
JBOSSに配置するトピック設定ファイルに、セキュリティ認証できるロールを設定する。
 
%JBOSS_HOME%\server\XXX\deploy\messaging\トピック設定ファイル.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<!--
    Messaging Example Destinations
-->
 
<server>
  <mbean code="org.jboss.jms.server.destination.TopicService"
     name="jboss.messaging.destination:service=Topic,name=testTopic"
     xmbean-dd="xmdesc/Topic-xmbean.xml">
     <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
     <depends>jboss.messaging:service=PostOffice</depends>
     <attribute name="SecurityConfig"> ←このSecurityConfigを削除する。
        <security>
<role name="test-role" read="true" write="true" create="false"/>←test-roleを追加(読み書き作成できるロール)
<role name="publisher" read="true" write="true" create="false"/>
           <role name="noacc" read="false" write="false" create="false"/>
        </security>
     </attribute>
  </mbean>
</server>
%JBOSS_HOME%\server\XXX\deploy\messaging\hsqldb-persistence-service.xml
~~ ↓のところを編集する。
<attribute name="SqlProperties"><![CDATA[
POPULATE.TABLES.1  = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('guest', 'guest')
POPULATE.TABLES.2  = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('j2ee', 'j2ee')
POPULATE.TABLES.3  = INSERT INTO JBM_USER (USER_ID, PASSWD, CLIENTID) VALUES ('john', 'needle', 'DurableSubscriberExample')
POPULATE.TABLES.4  = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('nobody', 'nobody')
POPULATE.TABLES.5  = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('dynsub', 'dynsub')
POPULATE.TABLES.6  = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('guest','guest')
POPULATE.TABLES.7  = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('j2ee','guest')
POPULATE.TABLES.8  = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('john','john')
POPULATE.TABLES.9  = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('subscriber','john')
POPULATE.TABLES.10 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('publisher','john')
POPULATE.TABLES.11 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('publisher','dynsub')
POPULATE.TABLES.12 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('durpublisher','john')
POPULATE.TABLES.13 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('durpublisher','dynsub')
POPULATE.TABLES.14 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('noacc','nobody')
POPULATE.TABLES.15 = INSERT INTO JBM_USER (USER_ID, PASSWD, CLIENTID) VALUES ('prm-user','password', 'prm-user')
POPULATE.TABLES.16 = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('ecm-user','password')
POPULATE.TABLES.17 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('prm-role','prm-user')
POPULATE.TABLES.18 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('ecm-role','ecm-user')
POPULATE.TABLES.19 = INSERT INTO JBM_USER (USER_ID, PASSWD) VALUES ('reffi', 'reffiPass')←ユーザの追加
POPULATE.TABLES.20 = INSERT INTO JBM_ROLE (ROLE_ID, USER_ID) VALUES ('reffi-role','reffi')←ロールの追加
     ]]></attribute>
 
※※※以下のように設定すると、クライアントIDも設定することができる。
POPULATE.TABLES.19 = INSERT INTO JBM_USER (USER_ID, PASSWD, CLIENTID) VALUES ('reffi', 'reffiPass', 'clientID')
 
ソース(DurableSubscriberの作成)
String topicName = "testTopic";
ObjectName serverObjectName = new ObjectName("jboss.messaging:service=ServerPeer");
TopicConnectionFactory tcf =
(TopicConnectionFactory)context.lookup("ConnectionFactory");
TopicConnection con = tcf.createTopicConnection("reffi", "reffiPass"); ←認証されているユーザで作成
con.setClientID("testConnection"); ←ClientIDが設定ファイルで指定されている場合はエラーになる。
Topic sampleTopic = (Topic)context.lookup("topic/" + topicName);
TopicSession session = con.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
con.start();
TopicSubscriber subscriber = session.createDurableSubscriber(sampleTopic, "subscriberName");
session.unsubscribe("subscriberName");
con.stop();
session.close();
con.close();
 

拍手[0回]

JBossCacheまとめ(ver4.3, ver5.1)

JBossCacheまとめ
    ・JBossとJBossCacheのバージョン対応表
        http://www.jboss.org/jbosscache/compatibility.html
        上記の表を見る限り、TreeCache系はJBoss5.1はJBossCache3.0.1以上、JBoss4.3は1.4.1あたりが入っていそう。
    ・マニュアル
        http://community.jboss.org/wiki/JBossCacheOfficialDocumentation

    ・主にツリー形式でキャッシュを登録するTreeCache(最新だとCoreCahe)、オブジェクト単位でキャッシュするPojoCacheがある。

JBoss4.3

    ・ライブラリは、jboss-eap-4.3\jboss-as\server\default\libなどの各サーバ設定に配置されている。
        jboss-cache-jdk50.jar

    .たぶんJBoss Cache1.4.1 "Cayenne", Core Edition(TreeCacheのこと)
        TreeCache は、JBoss Cache のツリー構造でレプリケートされたトランザクション対応キャッシュです。
        サーブレットコンテナだけでなくアプリケーションサーバ内から実行しない Java アプリケーションにも組み込むことができます。
        HashMap構造のメモリキャッシュで、オブジェクトサイズが非常に大きい場合、 1 つのフィールド更新だけでもオブジェクト全体のシリアライズを引き起こします。
        JBossサーバ内部のキャッシュ処理に使われている。

        package hoge;

        import org.jboss.cache.TreeCache;

        public class SampleTreeCache {

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

                TreeCache cache = new TreeCache();
                cache.setClusterName("JBossTreeCache");
                cache.setCacheMode(TreeCache.REPL_SYNC);
               
                cache.createService();
                cache.startService();
               
                // 追加
                cache.put("/root/cache", "key1", "value1");
                if (cache.exists("/root/cache", "key1")){
                    String result = (String)cache.get("/root/cache", "key1");
                    System.out.println("result = " + result);
                }

                // 削除
                cache.remove("/root/cache", "key1");
                if (!cache.exists("/root/cache", "key1")){
                    System.out.println("remove ok!!!");
                }
            }
        }

    ・たぶんJBoss Cache1.4.1 "Cayenne", POJO Edition

        TreeCacheがHashMap構造なのに対し、オブジェクト単位でキャシュできる。

        PojoCache用クラス
        package hoge;

        import java.io.Serializable;

        public class Person implements Serializable{

            private static final long serialVersionUID = -3534465237251990512L;

            private String name;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }

        実行クラス
        package hoge;

        import org.jboss.cache.aop.PojoCache;

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

                PojoCache cache = new PojoCache();
                Person bob = new Person();
                bob.setName("bob");

                cache.start();
               
                // 追加
                cache.putObject("/mycache/person", bob);

                // 取得
                Person result = (Person)cache.getObject("/mycache/person");
                System.out.println("Person = "  + result.getName());
               
                // 削除
                cache.remove("/mycache/person");
                Person result2 = (Person)cache.getObject("/mycache/person");
                if (result2 == null){
                    System.out.println("Person = 存在しない");
                }
               
                cache.stop();
                cache.destroy();
            }
        }

JBoss5.1

    ・ライブラリは、jboss-5.1.0.GA\server\all\lib以下に配置されている。
        defaultのサーバ設定に、allのライブラリを3つ追加するだけでも動作した。
        ・jbosscache-core.jar
        ・jbosscache-pojo.jar
        ・jgroups.jar

    ・たぶんJBoss Cache3.0.3.Nega, Core Edition(TreeCacheからCacheに名前変更されている)

        import java.util.List;
        import org.jboss.cache.Cache;
        import org.jboss.cache.CacheFactory;
        import org.jboss.cache.DefaultCacheFactory;
        import org.jboss.cache.Fqn;
        import org.jboss.cache.Node;

        public class SampleCache {
            public static void main(String[] args) {
                CacheFactory<String, String> factory = new DefaultCacheFactory<String, String>();
                Cache<String, String> sampleCache = factory.createCache();
                Node<String, String> rootNode = sampleCache.getRoot();
                Fqn<String> place1 = Fqn.fromString("/root/cache/place");
               
                // 追加
                Node<String, String> place1Node = rootNode.addChild(place1);
                place1Node.put("key1", "value1");
               
                // 取得方法
                System.out.println("key1" + place1Node.get("key1"));
                System.out.println("key1'" + sampleCache.get(place1, "key1"));

                // 削除
                place1Node.remove("key1");
            }
        }

    ・たぶんJBoss Cache3.0.0.GA, POJO Edition
       
        PojoCache用クラス
            package hoge;

            import java.io.Serializable;

            @org.jboss.cache.pojo.annotation.Replicable
            public class Person implements Serializable{

                private static final long serialVersionUID = -3534465237251990512L;

                private String name;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }
            }

        実行クラス
            package hoge;

            import org.jboss.cache.config.Configuration;
            import org.jboss.cache.pojo.PojoCache;
            import org.jboss.cache.pojo.PojoCacheFactory;
            public class SamplePojoCache {

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

                    Configuration config = new Configuration();
                    PojoCache cache = PojoCacheFactory.createCache(config, false);
                    Person bob = new Person();
                    bob.setName("bob");

                    cache.start();
                   
                    // 追加
                    cache.attach("person1", bob);

                    // 取得
                    Person result = (Person)cache.find("person1");
                    System.out.println("Person = "  + result.getName());
                   
                    // 削除
                    cache.detach("person1");
                    Person result2 = (Person)cache.find("person1");
                    if (result2 == null){
                        System.out.println("Person = 存在しない");
                    }
                   
                    cache.stop();
                    cache.destroy();
                }
            }


拍手[0回]

JBossCacheサンプル(JBoss5.1)

JBoss5.1でJBossCacheを使用してみた。

各JBossとJBossCacheのバージョン対応(JBoss5までしか載っていない。。)
http://www.jboss.org/jbosscache/compatibility.html

JBossCacheのドキュメント
http://community.jboss.org/wiki/JBossCacheOfficialDocumentation

サンプルを作成してみた。
こんな感じ。

package hoge;

import org.jboss.cache.Cache;
import org.jboss.cache.CacheFactory;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Node;

public class SampleCache {

    /**
     * @param args
     */
    public static void main(String[] args) {
        CacheFactory<String, String> factory = new DefaultCacheFactory<String, String>();
        Cache<String, String> sampleCache = factory.createCache();
       
        Node<String, String> rootNode = sampleCache.getRoot();
        Fqn<String> place1 = Fqn.fromString("/root/place1");
       
        // 追加
        Node<String, String> place1Node = rootNode.addChild(place1);
        place1Node.put("key1", "value1");
        place1Node.put("key2", "value2");
        place1Node.put("key3", "value3");
      
        // 取得方法1
        System.out.println("key1" + place1Node.get("key1"));
        System.out.println("key2" + place1Node.get("key2"));
        System.out.println("key3" + place1Node.get("key3"));
        System.out.println("key4" + place1Node.get("key4"));
        System.out.println("");

        // 取得方法2
        System.out.println("key1'" + sampleCache.get(place1, "key1"));
        System.out.println("key2'" + sampleCache.get(place1, "key2"));
        System.out.println("key3'" + sampleCache.get(place1, "key3"));
        System.out.println("key4'" + sampleCache.get(place1, "key4"));
       
        if (place1Node.get("key1") != null){
            System.out.println("Key1存在します");
        } else{
            System.out.println("Key1存在しません");
        }
       
        // 置き換え
        place1Node.put("key1", "newValue1");
        System.out.println("key1" + place1Node.get("key1"));
        System.out.println("key2" + place1Node.get("key2"));
        System.out.println("key3" + place1Node.get("key3"));
        System.out.println("key4" + place1Node.get("key4"));
        System.out.println("");
       
       
        // 削除
        place1Node.remove("key1");
        System.out.println("key1" + place1Node.get("key1"));
        System.out.println("key2" + place1Node.get("key2"));
        System.out.println("key3" + place1Node.get("key3"));
        System.out.println("key4" + place1Node.get("key4"));
        System.out.println("");

        if (place1Node.get("key1") != null){
            System.out.println("Key1存在します");
        } else{
            System.out.println("Key1存在しません");
        }

    }

}


拍手[0回]

JBOSS5だと、サーバランタイムが作成できない。

サーバランタイム登録時にmail.jarが見つからないと出る。
以下の組み合わせだと動作するみたいだ。

×Eclipse3.4.1 + JBOSS5.1.0
◎Eclipse3.4.2 + JBOSS5.1.0
◎Eclipse3.4.1 + JST Server Adapters3.0.4(3.0.2からUPDATE) + JBOSS5.1.0

拍手[0回]

| HOME |