/ Published in: Groovy
                    
                                        
ActiveMQ 5.8 Groovy Embeded Broker Example - Laurence Toenjes - 5/14/2013
This example overcomes some limitations of the basic ActiveMQ embedded
brokers examples I found online
	
Some of the challenges were:
# Multiple instances on same machine and be able to use JMX.
# Running on a machine with less than 50G or 100G disk space
caused combinations of ActiveMQ errors or warnings.
# Groovy Grapes/Grab syntax to use that would work on pc and mac.
		
The broker in this example uses a nonpersistent store and
is multicast discoverable and should allow you to run multiple instances
of it (in separate processes of course) which is the reason for all the
code snips containing random port nums and random thread sleeps
to increase the odds of success of each new embedded broker process
to get a working set of port nums.
                This example overcomes some limitations of the basic ActiveMQ embedded
brokers examples I found online
Some of the challenges were:
# Multiple instances on same machine and be able to use JMX.
# Running on a machine with less than 50G or 100G disk space
caused combinations of ActiveMQ errors or warnings.
# Groovy Grapes/Grab syntax to use that would work on pc and mac.
The broker in this example uses a nonpersistent store and
is multicast discoverable and should allow you to run multiple instances
of it (in separate processes of course) which is the reason for all the
code snips containing random port nums and random thread sleeps
to increase the odds of success of each new embedded broker process
to get a working set of port nums.
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
#!/usr/bin/env groovy
@Grapes([
])
// file: EmbeddedBroker.groovy
/*
ActiveMQ 5.8 Groovy Embeded Broker Example - Laurence Toenjes - 5/14/2013
This example overcomes some limitations of the basic ActiveMQ embedded
brokers examples I found online
Some of the challenges were:
# Multiple instances on same machine and be able to use JMX.
# Running on a machine with less than 50G or 100G disk space
caused combinations of ActiveMQ errors or warnings.
# Groovy Grapes/Grab syntax to use that would work on pc and mac.
The broker in this example uses a nonpersistent store and
is multicast discoverable and should allow you to run multiple instances
of it (in separate processes of course) which is the reason for all the
code snips containing random port nums and random thread sleeps
to increase the odds of success of each new embedded broker process
to get a working set of port nums.
*/
import org.apache.activemq.transport.discovery.multicast.MulticastDiscoveryAgent; // http://activemq.apache.org/maven/apidocs/src-html/org/apache/activemq/transport/discovery/multicast/MulticastDiscoveryAgent.html#line.54
//
/*
SET _JAVA_OPTIONS=-Dcom.sun.management.jmxremote ^
-Dcom.sun.management.jmxremote.port=51001 ^
-Dcom.sun.management.jmxremote.local.only=false ^
-Dcom.sun.management.jmxremote.authenticate=false ^
-DmyJmxConnectorPort=41001
*/
sockets = [];
ports = [];
// lets make activemq port same as pid so easy to use jconsole
ports[-1] = javaPid
ports; // return
}
calcPorts();
try {
// sockets = ports.collect { new Socket(it) }
}
}
sockets.clear();
calcPorts();
}
}
sm.'com.sun.management.jmxremote.port' = ports[0].toString()
sm.'com.sun.management.jmxremote.local.only' = 'false'
sm.'com.sun.management.jmxremote.authenticate' = 'false'
sm.'myJmxConnectorPort' = ports[1].toString()
// ports[0] is for com.sun.management.jmxremote.port
// ports[1] is for broker.getManagementContext().setConnectorPort
BrokerService broker
tryCounter = 100;
try {
broker = createBroker();
// run forever
lock.wait();
}
println "### Oops: ${ex}"
}
} // end while
}
// Stop ActiveMQ 5.8 Errors or Warnings when running on machines with
// less than 50G to 100G of diskspace
Long HundredGig = 107374182400L
}
broker.systemUsage.tempUsage.limit = fileVisitor.usableSpace/2;
broker.systemUsage.storeUsage.limit = fileVisitor.usableSpace/2;
}
// String theBrokerSuffix = sJavaPid.replace('@','_');
broker.brokerName = 'broker1'
// sometimes set in bat/sh starter
broker.getManagementContext().setConnectorPort( myJmxConnectorPort );
// !!! for jmx usage
broker.setBrokerObjectName(
BrokerMBeanSupport.createBrokerObjectName(broker.getManagementContext().getJmxDomainName(), broker.brokerName)
)
def conn = broker.addConnector("tcp://0.0.0.0:${calcMqPort()}"); // use 0.0.0.0 , makes discovery work better
// conn.name += "_port_${javaPid}"
// for discovery
conn.discoveryUri = new URI( "${C_DEFAULT_DISCOVERY_URI_STRING}?useLocalHost=false".trim() ); // optional add ?
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                