⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08s07.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 4 页
字号:
  /**
   * Run an example subscribing to topic testTopic.
   * Only works up to and including JBoss 2.4.0
   */
  public static void main(String[] args) {

    try {

      // Create the HelloSubscriber, giving it the name of the
      // TopicConnection Factory and the Topic destination to use in
      // lookup.
      HelloSubscriber subscriber = new HelloSubscriber(
        // Name of ConnectionFactory
        "TopicConnectionFactory",
        // Name of destination to publish to
        "topic/testTopic");

    } catch(Exception ex) {

      System.err.println(
        "An exception occurred while testing HelloSubscriber: " + ex);
      ex.printStackTrace();

    }

  }

} // HelloSubscriber

</pre></div><p>
You will find the files <tt>HelloSubscriber.java</tt> and <tt>HelloSubscriber25.java</tt> in the same directory as HelloPublisher; note that <tt>HelloSubscriber25.java</tt> only works with JBoss version 2.4.1. There are also in the same directory complete examples of how to work with queues in the files <tt>HelloSender.java</tt> and <tt>HelloReceiver.java</tt>.
</p><p>
The examples may be run and tested with the provided Ant build file <tt>build.xml</tt>.
</p><p>
To run the topic examples, open two terminal windows (or virtual sessions) and change to the <tt>build</tt> directory. In the first terminal window type:
</p><div class="figure"><p><a name="d0e4419"></a><b>Figure 8.32. Running the HelloSubscriber example</b></p><pre class="programlisting">
ant jms-hello-subscriber

</pre></div><p>
If all went well, you should see a message at the end looking like this:
</p><div class="figure"><p><a name="d0e4426"></a><b>Figure 8.33. Output of running the HelloSubscriber example</b></p><pre class="programlisting">
jms-hello-subscriber:
     [echo] Using resources from: /home/pra/src/examples/resources
     [java] HelloSubscriber subscribed to topic: topic/testTopic

</pre></div><p>
Now, type in the other window:
</p><div class="figure"><p><a name="d0e4433"></a><b>Figure 8.34. Running the HelloPublisher example</b></p><pre class="programlisting">
ant jms-hello-publisher

</pre></div><p>
In the subscriber window you should now see:
</p><div class="figure"><p><a name="d0e4440"></a><b>Figure 8.35. Output of running the HelloPublisher example</b></p><pre class="programlisting">
     [java] HelloSubscriber got message: Hello World no. 1
     [java] HelloSubscriber got message: Hello World no. 2
     [java] HelloSubscriber got message: Hello World no. 3
     [java] HelloSubscriber got message: Hello World no. 4
     [java] HelloSubscriber got message: Hello World no. 5
     [java] HelloSubscriber got message: Hello World no. 6
     [java] HelloSubscriber got message: Hello World no. 7
     [java] HelloSubscriber got message: Hello World no. 8
     [java] HelloSubscriber got message: Hello World no. 9
     [java] HelloSubscriber got message: Hello World no. 10

</pre></div><p>
To run a JMS client with JBossMQ without the help of the already defined <tt>build.xml</tt> file you will need to set up a correct classpath. The library files needed to run a client are actually different for all the treated versions of JBoss. Among the examples, under the <tt>org/jboss/docs/jms/bin</tt> directory, there are three shell scripts that show how to run a JMS client for each of the different versions of JBoss.
</p><p>
First of all, you need to have the classes of your own client in the classpath. You should also have included in the classpath the directory where your <tt>jndi.properties</tt> file is located.
</p><p>
You will also need in the classpath the following JAR files included in the standard JBoss distribution:
</p><div class="itemizedlist"><ul><li><p><a name="d0e4461"></a>client/jbossmq-client.jar</p></li><li><p><a name="d0e4464"></a>client/jnp-client.jar</p></li><li><p><a name="d0e4467"></a>client/jta-spec1_0_1.jar</p></li><li><p><a name="d0e4470"></a>lib/ext/jms.jar</p></li></ul></div><p>[<span class="bold"><b>2.4.0</b></span>. You should have these, instead:</p><div class="itemizedlist"><ul><li><p><a name="d0e4479"></a>client/jbossmq-client.jar</p></li><li><p><a name="d0e4482"></a>client/jnp-client.jar</p></li><li><p><a name="d0e4485"></a>client/jta-spec1_0_1.jar</p></li><li><p><a name="d0e4488"></a>client/jboss-j2ee.jar</p></li><li><p><a name="d0e4491"></a>lib/ext/oswego-concurrent.jar]</p></li></ul></div><p>[<span class="bold"><b>2.4.1</b></span>. You should have these, instead:</p><div class="itemizedlist"><ul><li><p><a name="d0e4500"></a>client/jbossmq-client.jar</p></li><li><p><a name="d0e4503"></a>client/jnp-client.jar</p></li><li><p><a name="d0e4506"></a>client/jta-spec1_0_1.jar</p></li><li><p><a name="d0e4509"></a>client/jboss-j2ee.jar</p></li><li><p><a name="d0e4512"></a>lib/ext/oswego-concurrent.jar</p></li><li><p><a name="d0e4515"></a>client/log4j.jar]</p></li></ul></div><p>
Here it is an example shell script to run the HelloPublisher client program:
</p><div class="figure"><p><a name="d0e4520"></a><b>Figure 8.36. Shell script for HelloPublisher from <tt>org/jboss/docs/jms/bin/runHelloPublisher22.sh</tt></b></p><pre class="programlisting">
#!/bin/bash

#
# Example script to run JMS client against JBoss 2.2.x
#

# Directory where jndi.properties is located
JNDI_RESOURCE_DIR=../resources/

# HelloPublisher class
CLIENT_CLASS_DIR=../../../../../build-examples/jms/classes/

# Required libs to run client
LIBS=$JBOSS_DIST/client/jbossmq-client.jar:\
$JBOSS_DIST/client/jnp-client.jar:\
$JBOSS_DIST/client/jta-spec1_0_1.jar:\
$JBOSS_DIST/lib/ext/jms.jar

# Agregated classpath
CLASSPATH=:$LIBS:\
$CLIENT_CLASS_DIR:\
$JNDI_RESOURCE_DIR

echo "Running with classpath $CLASSPATH"
$JAVA_HOME/bin/java -classpath $CLASSPATH org.jboss.docs.jms.client.HelloPublisher

</pre></div><p>
To use this you have to set the environment variables <tt>JBOSS_DIST</tt> and <tt>JAVA_HOME</tt> as explained in <a href="ch08s02.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s02.html" title="Introduction">the section called &#8220;Introduction&#8221;</a>
			</p><p>
Here are the Ant targets to run the JMS examples:
</p><div class="figure"><p><a name="d0e4539"></a><b>Figure 8.37. Ant targets for JMS examples</b></p><pre class="programlisting">
ant jms-hello-publisher
ant jms-hello-subscriber
ant jms-hello-publisher25
ant jms-hello-subscriber25
ant jms-hello-sender
ant jms-hello-receiver
ant jms-hello-sender25
ant jms-hello-receiver25
ant jms-manual-jndi

</pre></div><p>
As a final touch we shall also explain a JBoss-specific feature: how to manage destinations on the fly.
 As of this writing, it is only possible to do this up to and including JBoss version 2.4.0. Is is also possible to do this in JBoss version 2.4.1, but the API has changed slightly and the created destinations are not persisted between server restarts.
</p><p>
There are two different ways of doing this, depending on whether the code is in the same Virtual Machine as JBoss or it is in a standalone client. Both involve invoking a JMX MBean registered by the name <tt>service=JMSServer</tt>; this MBean has four methods to manage destinations: newTopic, destroyTopic, newQueue and destroyQueue.
</p><p>[<span class="bold"><b>2.4.1</b></span>. The MBean is registered by the name <tt>service=Server</tt> and its method names are createTopic, destroyTopic, createQueue and destroyQueue.]</p><p>
The difference between the two ways of managing destinations on the fly is reflected in how the MBean is invoked. If the code is in the same VM as the MBean server, you may invoke it directly. To create a topic destination you might do it like this:
</p><div class="figure"><p><a name="d0e4561"></a><b>Figure 8.38. Creating a topic programmatically in the same VM</b></p><pre class="programlisting">
// Get the MBean server
MBeanServer server = (MBeanServer)
  MBeanServerFactory.findMBeanServer(null).iterator().next();

// Invoke the MBean
server.invoke(new ObjectName("JMS", "service", "JMSServer"),
  "newTopic",
  new Object[] { "myTopic" },
  new String[] { "java.lang.String" });

</pre></div><p>[<span class="bold"><b>2.4.1</b></span>. Because of the changes in the strings used to look up the MBean and in the method names of the MBean, it would typically look like below, instead.]</p><div class="figure"><p><a name="d0e4571"></a><b>Figure 8.39. [<span class="bold">2.4.1</span>] Creating a topic programmatically in the same VM</b></p><pre class="programlisting">
// Get the MBean server
MBeanServer server = (MBeanServer)
  MBeanServerFactory.findMBeanServer(null).iterator().next();

// Invoke the MBean
server.invoke(new ObjectName("JBossMQ", "service", "Server"),
  "createTopic",
  new Object[] { "myTopic" },
  new String[] { "java.lang.String" });

</pre></div><p>
If the code is not in the same VM as the MBean server, you need to go through a JMX adapter. One JMX adapter is the HTML GUI. It is possible to programmatically invoke an MBean via a URL.
</p><div class="figure"><p><a name="d0e4581"></a><b>Figure 8.40. Creating a topic programmatically via a URL</b></p><pre class="programlisting">
//These are samples, not available in the example code.
String method = "newTopic";
String destName = "myCreated";

String action = "action=" + method + "?action=" + method +
  "&amp;param0%2Bjava.lang.String=" + destName;

String arg = "/InvokeAction//JMS%3Aservice%3DJMSServer" + "/" + action;

URL url = new URL("http", "localhost", 8082, arg);

//Invoke the URL
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.connect();

</pre></div><p>[<span class="bold"><b>2.4.1</b></span>. The strings used would look somewhat different.]</p><div class="figure"><p><a name="d0e4591"></a><b>Figure 8.41. [<span class="bold">2.4.1</span>] Creating a topic programmatically via a URL</b></p><pre class="programlisting">
//These are samples, not available in the example code.
String method = "createTopic";
String destName = "myCreated";

String action = "action=" + method + "?action=" + method +
  "&amp;param0%2Bjava.lang.String=" + destName;

String arg = "/InvokeAction//JBossMQ%3Aservice%3DServer" + "/" + action;

URL url = new URL("http", "localhost", 8082, arg);

//Invoke the URL
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.connect();

</pre></div><p>
The example code may be found in the source file <tt>DestinationHelper.java</tt> in directory <tt>org/jboss/docs/jms/client</tt>.</p><p>[<span class="bold"><b>2.4.1</b></span>. The source file is <tt>DestinationHelper25.java</tt> in the same directory.]</p><p>You may run it with the Ant targets:
</p><div class="figure"><p><a name="d0e4617"></a><b>Figure 8.42. Running the DestinationHelper example</b></p><pre class="programlisting">
ant jms-create-dest
ant jms-destroy-dest

</pre></div><div class="figure"><p><a name="d0e4622"></a><b>Figure 8.43. [<span class="bold">2.4.1</span>] Running the DestinationHelper example</b></p><pre class="programlisting">
ant jms-create-dest25
ant jms-destroy-dest25

</pre></div><p>
They will create and destroy the topic <tt>myCreated</tt>.
</p></div></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch08.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch08s03.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s03.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch08s20.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s20.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -