ナビゲーションをスキップ

WebLogic JMS プログラマーズ ガイド

  前 次 前/次ボタンと目次ボタンとの区切り線 目次  

JMS ヘルパーを使用したアプリケーションの管理

weblogic.jms.extensions.JMSHelper クラスには、JMS リソースをプログラムで作成するのに使用できる API が格納されています。

 


JMSHelper を使用した JMS リソースのコンフィグレーション

JMSHelper は、以下の API シグネチャを使用して、キューやトピックなどの JMS リソースを管理します。

JNDI 初期コンテキスト、送り先に関連付けられる JMS サーバの名前、送り先 (キューまたはトピック) の名前、および JNDI ネームスペース内で送り先をルックアップする場合に使用する名前を指定する必要があります。

各メソッドによって、以下のものが更新されます。

JMS サーバの詳細については、「JMS : コンフィグレーション」を参照してください。

 


JMSHelper サンプル コード

この節では、JMS リソースを作成および削除するサンプル コードを示します。

JMS リソースの作成

以下のコードで、JMS トピックが作成されます。

コード リスト 7-1 JMS トピックの作成

package examples.jms.topic;

import java.util.*;
import javax.jms.*;
import javax.naming.*;
import weblogic.management.WebLogicMBean;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.*;
import weblogic.management.configuration.*;
import weblogic.jms.extensions.JMSHelper;
import weblogic.jndi.Environment;

public class MBeanConfig
{
   public MBeanConfig() {
   }

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

   String url, username, password;
   if (args.length != 0 && args.length != 3) {
   System.out.println("Usage: MBeanConfig <url> <username> <password>");
   System.exit(1);
   }

   if (args.length == 0) {
      url = new String("t3://localhost:7001");
      username = "weblogic";
      password = "weblogic";
   } else {
      url = args[0];
      username = args[1];
      password = args[2];
   }
   Context ctx = getInitialContext(url, username, password);
   MBeanConfig mbt = new MBeanConfig();

   createJMSUsingJMSHelper(ctx);
}

   private static void createJMSUsingJMSHelper(
      Context ctx
   ){
   System.out.println("\n\n.... Configure a JMS Topic ....\n\n");
   try {
      MBeanHome mbeanHome =
      (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
      DomainMBean domainMBean = mbeanHome.getActiveDomain();
      String domainMBeanName = domainMBean.getName();
      ServerMBean[] servers = domainMBean.getServers();

/**
  * トピックの作成
  */
      String jmsServerName = "examplesJMSServer";
      String topicName = "exampleTopic";
      String topicjndiName = "weblogic.examples.jms.exampleTopic";
      JMSHelper.createPermanentTopicAsync(
         ctx,
         jmsServerName,
         topicName,
         topicjndiName);
 System.out.println("Created Topic " + JMSHelper.getJMSTopicConfigMBean(
            ctx,
            jmsServerName,
            topicName
            ));
         } catch (Exception e) {
   System.out.println("Example configuration failed :" + e.getMessage());
      e.printStackTrace();
      }
   }

private static Context getInitialContext(
   String url,
   String username,
   String password
   ) throws Exception {
      Environment env = new Environment();
      env.setProviderUrl(url);
      env.setSecurityPrincipal(username);
      env.setSecurityCredentials(password);
      Context c = env.getInitialContext();
      return c;
   }
}

JMS リソースの削除

以下のコードで、JMS トピックが削除されます。

コード リスト 7-2 JMS トピックの削除

package examples.jms.topic;

import java.util.*;
import javax.jms.*;
import javax.naming.*;

import weblogic.management.WebLogicMBean;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.*;
import weblogic.management.configuration.*;

import weblogic.jms.extensions.JMSHelper;
import weblogic.jndi.Environment;

public class MBeanClean
{
   public MBeanClean() {
}

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

   String url, username, password;

   if (args.length != 0 && args.length != 3) {
      System.out.println("Usage: MBeanClean <url> <username> <password>");
      System.exit(1);
}

if (args.length == 0) {
   url = new String("t3://localhost:7001");
   username = "system";
   password = "gumby1234";
   } else {
      url = args[0];
      username = args[1];
      password = args[2];
   }

   Context ctx = getInitialContext(url, username, password);
   MBeanClean mbt = new MBeanClean();
   deleteJMSUsingJMSHelper(ctx);
}

private static void deleteJMSUsingJMSHelper(
   Context ctx
   ) {
   System.out.println("\n\n.... Remove a JMS Topic ....\n\n");
   try {
      MBeanHome mbeanHome =
         (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
         DomainMBean domainMBean = mbeanHome.getActiveDomain();
         String domainMBeanName = domainMBean.getName();
         ServerMBean[] servers = domainMBean.getServers();

   String jmsServerName = "examplesJMSServer";

/**
 * トピックの削除
 */
   String topicName = "exampleTopic";
   JMSHelper.deletePermanentTopic(
      ctx,
      jmsServerName,
      topicName
      );
      } catch (Exception e) {
  System.out.println("Example configuration failed :" + e.getMessage());
  e.printStackTrace();
  }
}

private static Context getInitialContext(
   String url,
   String username,
   String password
   ) throws Exception {
      Environment env = new Environment();
      env.setProviderUrl(url);
      env.setSecurityPrincipal(username);
      env.setSecurityCredentials(password);
      Context c = env.getInitialContext();
      return c;
   }

 


JMSHelper 使用時のベスト プラクティス

この節では、JMSHelper を使用して JMS サーバおよびリソースをコンフィグレーションする場合のベスト プラクティス情報を示します。

private static Queue findQueue (
QueueSession queueSession,
String jmsServerName,
String queueName,
int retryCount,
long retryInterval
) throws JMSException
{
String wlsQueueName = jmsServerName + "/" + queueName;
String command = "QueueSession.createQueue(" +
wlsQueueName + ")";
long startTimeMillis = System.currentTimeMillis();
for (int i=retryCount; i>=0; i--) {
try {
System.out.println("Trying " + command);
Queue queue = queueSession.createQueue(wlsQueueName);
System.out.println(command + "succeeded after " +
(retryCount - i + 1) + " tries in " +
(System.currentTimeMillis() - startTimeMillis) +
" millis.");
return queue;
} catch (JMSException je) {
if (retryCount == 0) throw je;
}
try {
System.out.println(command + "> failed, pausing " +
retryInterval + " millis.");
Thread.sleep(retryInterval);
} catch (InterruptedException ignore) {}
   }
throw new JMSException("out of retries");
}

この場合、JMSHelper クラス メソッドの後に findQueue() メソッドを呼び出すことで、動的に作成されたキューを使用可能になりしだい、取り出すことができます。例を示します。

JMSHelper.createPermanentQueueAsync(ctx, domain, jmsServerName,  
queueName, jndiName);
Queue queue = findQueue(qsess, jmsServerName, queueName,
retry_count, retry_interval);

JMSHelper クラスの詳細については、「weblogic.jms.extensions.JMSHelper」Javadoc を参照してください。

 

フッタのナビゲーションのスキップ  ページの先頭 前 次