added an example of running an embedded broker in Java code

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@366477 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-01-06 11:29:28 +00:00
parent 6285f96b15
commit be19f9aabd
2 changed files with 174 additions and 126 deletions

View File

@ -16,154 +16,163 @@
--> -->
<project name="Inbox" default="help" basedir="."> <project name="Inbox" default="help" basedir=".">
<property name="class.dir" value="target/classes"/> <property name="class.dir" value="target/classes" />
<!-- example program defaults --> <!-- example program defaults -->
<property name="url" value="tcp://localhost:61616"/> <property name="url" value="tcp://localhost:61616" />
<property name="topic" value="false"/> <property name="topic" value="false" />
<property name="subject" value="TEST.FOO"/> <property name="subject" value="TEST.FOO" />
<property name="durable" value="false"/> <property name="durable" value="false" />
<property name="max" value="10"/> <property name="max" value="10" />
<property name="messageSize" value="1000"/> <property name="messageSize" value="1000" />
<property name="clientId" value="consumer1"/> <property name="clientId" value="consumer1" />
<property name="producerClientId" value="null"/> <property name="producerClientId" value="null" />
<property name="timeToLive" value="0"/> <property name="timeToLive" value="0" />
<property name="sleepTime" value="0"/> <property name="sleepTime" value="0" />
<property name="transacted" value="false"/> <property name="transacted" value="false" />
<!-- for WAR --> <!-- for WAR -->
<property name="app.name" value="activemq-web"/> <property name="app.name" value="activemq-web" />
<property name="app.base.dir" location="target/${app.name}"/> <property name="app.base.dir" location="target/${app.name}" />
<property name="app.webinf.dir" value="${app.base.dir}/WEB-INF"/> <property name="app.webinf.dir" value="${app.base.dir}/WEB-INF" />
<property name="app.classes.dir" value="${app.webinf.dir}/classes"/> <property name="app.classes.dir" value="${app.webinf.dir}/classes" />
<property name="app.lib.dir" value="${app.webinf.dir}/lib"/> <property name="app.lib.dir" value="${app.webinf.dir}/lib" />
<property name="app.src.dir" value="src"/> <property name="app.src.dir" value="src" />
<property name="app.conf.dir" value="conf"/> <property name="app.conf.dir" value="conf" />
<property name="app.dist.dir" value="target"/> <property name="app.dist.dir" value="target" />
<target name="help"> <target name="help">
<echo> <echo>
This script requires Ant 1.6 or higher This script requires Ant 1.6 or higher
usage: usage:
ant -help display ant help screen ant -help display ant help screen
ant help display this message ant help display this message
ant clean delete the built directory ant clean delete the built directory
ant consumer creates a consumer which waits until a specific number of messages have been received ant consumer creates a consumer which waits until a specific number of messages have been received
ant producer creates a producer publishing a number of messages ant producer creates a producer publishing a number of messages
ant embedBroker runs an embedded broker inside Java code
ant war creates a WAR deployment unit of the ActiveMQ Broker ant war creates a WAR deployment unit of the ActiveMQ Broker
</echo> </echo>
</target> </target>
<target name="clean"> <target name="clean">
<delete dir="target" quiet="true"/> <delete dir="target" quiet="true" />
<delete dir="${class.dir}" quiet="true"/> <delete dir="${class.dir}" quiet="true" />
</target> </target>
<target name="init"> <target name="init">
<mkdir dir="${class.dir}"/> <mkdir dir="${class.dir}" />
<mkdir dir="src/ddl"/> <mkdir dir="src/ddl" />
<path id="javac.classpath"> <path id="javac.classpath">
<pathelement path="${class.dir}"/> <pathelement path="${class.dir}" />
<pathelement path="../conf"/> <pathelement path="../conf" />
<fileset dir="../lib"> <fileset dir="../lib">
<include name="**/*.jar"/> <include name="**/*.jar" />
</fileset> </fileset>
</path> </path>
</target> </target>
<target name="compile" depends="init" description="Compile all Java"> <target name="compile" depends="init" description="Compile all Java">
<javac srcdir="src" destdir="${class.dir}"> <javac srcdir="src" destdir="${class.dir}">
<classpath refid="javac.classpath"/> <classpath refid="javac.classpath" />
</javac> </javac>
<copy todir="${class.dir}"> <copy todir="${class.dir}">
<fileset dir="src"> <fileset dir="src">
<include name="**/*.properties"/> <include name="**/*.properties" />
<include name="**/*.xml"/> <include name="**/*.xml" />
</fileset> </fileset>
</copy> </copy>
</target> </target>
<target name="war" depends="compile" description="Create the activemq broker war file."> <target name="war" depends="compile" description="Create the activemq broker war file.">
<!--Copy the required jar files--> <!--Copy the required jar files-->
<copy todir="${app.lib.dir}"> <copy todir="${app.lib.dir}">
<fileset dir="../lib"> <fileset dir="../lib">
<include name="*.jar"/> <include name="*.jar" />
<exclude name="servlet.jar"/> <exclude name="servlet.jar" />
</fileset> </fileset>
<fileset dir="../lib/optional"> <fileset dir="../lib/optional">
<include name="activemq-web*.jar"/> <include name="activemq-web*.jar" />
<include name="activemq-container*.jar"/> <include name="activemq-container*.jar" />
<include name="commons-bean*.jar"/> <include name="commons-bean*.jar" />
<!-- include Apache Derby for persistence --> <!-- include Apache Derby for persistence -->
<include name="derby*.jar"/> <include name="derby*.jar" />
<!-- include database pooling just in case --> <!-- include database pooling just in case -->
<include name="commons-pool*.jar"/> <include name="commons-pool*.jar" />
<include name="commons-db*.jar"/> <include name="commons-db*.jar" />
</fileset> </fileset>
</copy> </copy>
<!--Copy the configuration file--> <!--Copy the configuration file-->
<copy todir="${app.webinf.dir}"> <copy todir="${app.webinf.dir}">
<fileset dir="${app.conf.dir}"> <fileset dir="${app.conf.dir}">
<!--Web application configuration file--> <!--Web application configuration file-->
<include name="web.xml"/> <include name="web.xml" />
<!--ActiveMQ configuration file--> <!--ActiveMQ configuration file-->
<include name="activemq.xml"/> <include name="activemq.xml" />
</fileset> </fileset>
</copy> </copy>
<copy todir="${app.classes.dir}"> <copy todir="${app.classes.dir}">
<fileset dir="${app.conf.dir}"> <fileset dir="${app.conf.dir}">
<!--Log4j configuration file--> <!--Log4j configuration file-->
<include name="log4j.properties"/> <include name="log4j.properties" />
</fileset> </fileset>
</copy> </copy>
<!-- Create the war file --> <!-- Create the war file -->
<jar jarfile="${app.dist.dir}/${app.name}.war" <jar jarfile="${app.dist.dir}/${app.name}.war" basedir="${app.base.dir}" />
basedir="${app.base.dir}"/> </target>
</target>
<target name="consumer" depends="compile" description="Runs a simple consumer"> <target name="consumer" depends="compile" description="Runs a simple consumer">
<echo>Running consumer against server at $$url = ${url} for subject $$subject = ${subject}</echo> <echo>Running consumer against server at $$url = ${url} for subject $$subject = ${subject}</echo>
<java classname="ConsumerTool" fork="yes" maxmemory="100M"> <java classname="ConsumerTool" fork="yes" maxmemory="100M">
<classpath refid="javac.classpath"/> <classpath refid="javac.classpath" />
<jvmarg value="-server"/> <jvmarg value="-server" />
<arg value="${url}"/> <arg value="${url}" />
<arg value="${topic}"/> <arg value="${topic}" />
<arg value="${subject}"/> <arg value="${subject}" />
<arg value="${durable}"/> <arg value="${durable}" />
<arg value="${max}"/> <arg value="${max}" />
<arg value="${clientId}"/> <arg value="${clientId}" />
<arg value="${transacted}"/> <arg value="${transacted}" />
<arg value="${sleepTime}"/> <arg value="${sleepTime}" />
</java> </java>
</target> </target>
<target name="producer" depends="compile" description="Runs a simple producer"> <target name="producer" depends="compile" description="Runs a simple producer">
<echo>Running producer against server at $$url = ${url} for subject $$subject = ${subject}</echo> <echo>Running producer against server at $$url = ${url} for subject $$subject = ${subject}</echo>
<java classname="ProducerTool" fork="yes" maxmemory="100M"> <java classname="ProducerTool" fork="yes" maxmemory="100M">
<classpath refid="javac.classpath"/> <classpath refid="javac.classpath" />
<jvmarg value="-server"/> <jvmarg value="-server" />
<arg value="${url}"/> <arg value="${url}" />
<arg value="${topic}"/> <arg value="${topic}" />
<arg value="${subject}"/> <arg value="${subject}" />
<arg value="${durable}"/> <arg value="${durable}" />
<arg value="${max}"/> <arg value="${max}" />
<arg value="${messageSize}"/> <arg value="${messageSize}" />
<arg value="${producerClientId}"/> <arg value="${producerClientId}" />
<arg value="${timeToLive}"/> <arg value="${timeToLive}" />
<arg value="${sleepTime}"/> <arg value="${sleepTime}" />
<arg value="${transacted}"/> <arg value="${transacted}" />
</java> </java>
</target> </target>
<target name="embedBroker" depends="compile" description="Runs a simple producer">
<echo>Running an embedded broker example</echo>
<java classname="EmbeddedBroker" fork="yes" maxmemory="100M">
<classpath refid="javac.classpath" />
<jvmarg value="-server" />
</java>
</target>
</project> </project>

View File

@ -0,0 +1,39 @@
import org.apache.activemq.broker.BrokerService;
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
/**
* This example demonstrates how to run an embedded broker inside your Java code
*
* @version $Revision$
*/
public class EmbeddedBroker {
public static void main(String[] args) throws Exception {
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
// now lets wait forever to avoid the JVM terminating immediately
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}