Preface
ACTIVEMQ In addition to being able to be deployed separately on the server as a standalone process, can also be very small embedded in the program to start, let us briefly describe the built-in broker launch several ways.
First of all to prepare the work or need to prepare the ACTIVEMQ jar package, please configure yourself, not much to repeat
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactid>activemq-all </artifactId>
<version>5.15.2</version>
</dependency>
first, the code starts directly
This method starts directly in the code, because it is relatively simple and is directly on the code:
Package com.tgb.SpringActivemq.test;
Import Org.apache.activemq.broker.BrokerService;
public class Innerbroker {public
static void Main (String args[]) throws Exception {
Brokerservice broker = new Br Okerservice ();
Enable the JMX monitoring feature for broker
BROKER.SETUSEJMX (true);
Set Broker name
broker.setbrokername ("Mybroker");
Whether to use persistent
broker.setpersistent (false);
Add the Connection Agreement, address
Broker.addconnector ("tcp://localhost:61616");
Broker.start ();
}
}
After the operation, you can pass sender and receiver test, please refer to the previous article
http://blog.csdn.net/csdn_kenneth/article/details/79093529
In this case, the most basic inline broker will start to complete.
Second, the Factory mode to start
the similar code to start directly, but through the Factory mode to read the configuration file, and then start. Therefore, you need to create a new properties file in the resources directory or the project root directory, this is called Broker.properties Bar, the content is:
Usejms=true
persistent=false
brokername=factorybroker
The content is the same as the code-initiated parameters, the code is:
Package com.tgb.SpringActivemq.test;
Import Java.net.URI;
Import org.apache.activemq.broker.BrokerFactory;
Import Org.apache.activemq.broker.BrokerService;
public class Innerfactorybroker {public
static void Main (String args[]) throws exception{
string uri = "Propertie S:broker.properties ";
Brokerservice broker = brokerfactory.createbroker (new Uri (URI));
Broker.addconnector ("tcp://localhost:61616");
Broker.start ();
}
}
This way you can change some of the parameters through the configuration file
third, spring start
Spring is a very powerful and useful tool, we all like to put some configuration or fixed start things in the inside, ACTIVEMQ is no exception. Using spring first needs to be remembered on dependencies:
<!--Spring Core--
<!--Http://mvnrepository.com/artifact/org.springframework/spring-core--
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-core</artifactid>
<version>4.1.4.RELEASE</version>
</dependency>
<!--Spring Context
--<!--Http://mvnrepository.com/artifact/org.springframework/spring-context-- >
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-context</artifactid>
<version>4.1.4.RELEASE</version>
</dependency>
Then create the configuration XML file, where we build a Beans.xml file:
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
< Bean id= "broker" class= "Org.apache.activemq.broker.BrokerService" init-method= "Start" destroy-method= "Stop" >
<property name= "Brokername" value= "Springbroker"/>
<property name= "Persistent" value= "false"/ >
<property name= "Transportconnectoruris" >
<list>
<value>tcp://localhost:61616 </value>
</list>
</property>
</bean>
</beans>
There are some basic parameters for configuring broker startup, and then the configuration is within spring, and the code is simple:
Package com.tgb.SpringActivemq.test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Springbroker {public
static void Main (String args[]) {
ApplicationContext ctx = new CLASSPATHXMLAPPL Icationcontext ("Applicationcontext.xml");
}
}
Transferred from: http://blog.csdn.net/Roy_70/article/details/78773470