WebLogic JMS プログラマーズ ガイド
![]() |
![]() |
![]() |
![]() |
weblogic.jms.extensions.JMSHelper クラスには、JMS リソースをプログラムで作成するのに使用できる API が格納されています。
JMSHelper は、以下の API シグネチャを使用して、キューやトピックなどの JMS リソースを管理します。
JNDI 初期コンテキスト、送り先に関連付けられる JMS サーバの名前、送り先 (キューまたはトピック) の名前、および JNDI ネームスペース内で送り先をルックアップする場合に使用する名前を指定する必要があります。
JMS サーバの詳細については、「JMS : コンフィグレーション」を参照してください。
この節では、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;
}
}
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 を使用して JMS サーバおよびリソースをコンフィグレーションする場合のベスト プラクティス情報を示します。
createQueue()
メソッドまたは createTopic()
メソッドを使用して、それぞれキューまたはトピックの存在をテストすることをお勧めします。この方法によって、伝播固有の遅延を、ある程度回避できます。
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.createPermanentQueueAsync(ctx, domain, jmsServerName,
queueName, jndiName);
Queue queue = findQueue(qsess, jmsServerName, queueName,
retry_count, retry_interval);
JMSHelper
クラスの詳細については、「weblogic.jms.extensions.JMSHelper」Javadoc を参照してください。
![]() ![]() |
![]() |
![]() |