# 使用 Maven 的 HornetQ 独立服务器示例

> 原文： [https://howtodoinjava.com/hornetq/hornetq-stand-alone-server-example-using-maven/](https://howtodoinjava.com/hornetq/hornetq-stand-alone-server-example-using-maven/)

[**HornetQ**](http://hornetq.jboss.org "hornetq") 是一个开放源代码项目，旨在构建多协议，可嵌入，非常高性能的集群异步消息传递系统。 HornetQ 支持 JMS 1.1 API，并且还定义了自己的消息传递 API，以实现最佳性能和灵活性。 HornetQ 一流的高性能日志以非持久消息传递通常看到的速度提供持久消息传递性能。 HornetQ 提供服务器复制和自动客户端故障转移功能，以消除服务器故障时丢失或重复的消息。

在这篇文章中，我们将学习将 hornetq 服务器作为独立服务器运行的最基本配置，即在诸如 jboss 之类的任何容器之外运行。

步骤 1）使用以下命令创建一个 maven 项目并将其转换为 Eclipse Java 项目

```java
mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HornetQHelloWorld
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 cd HornetQHelloWorld

 mvn eclipse:eclipse
```

步骤 2）更新`pom.xml`文件并更新项目依赖项

**`pom.xml`**

```java
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>com.howtodoinjava</groupid>
  <artifactid>HornetQHelloWorld</artifactid>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>HornetQHelloWorld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-core</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-jms</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-logging</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-transports</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.jboss.netty</groupid>
     <artifactid>netty</artifactid>
     <version>3.1.0.GA</version>
  </dependency>
  <dependency>
     <groupid>org.jboss.javaee</groupid>
     <artifactid>jboss-jms-api</artifactid>
     <version>1.1.0.GA</version>
     <scope>compile</scope>
  </dependency>
  </dependencies>

```

步骤 3）将基本的 hornetq 配置文件放在类路径中。

**`hornetq-configuration.xml`**

```java
< ?xml version="1.0"?>
xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
	<connectors>
		<connector name="netty-connector">
			<factory -class>org.hornetq.integration.transports.netty.NettyConnectorFactory
			</factory>
		</connector>
	</connectors>
	<acceptors>
		<acceptor name="netty-acceptor">
			<factory -class>org.hornetq.integration.transports.netty.NettyAcceptorFactory
			</factory>
		</acceptor>
	</acceptors>
	<security -enabled>false</security>

```

**步骤 4）**配置连接器工厂，并将配置文件放置在 classpath 中。

**hornetq-jms.xml**

```java
< ?xml version="1.0"?>
xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
	<!--the connection factory used by the example -->
	<connection -factory name="ConnectionFactory">
		<connectors>
			<connector -ref connector-name="netty-connector"></connector>
		</connectors>
		<entries>
			<entry name="ConnectionFactory"></entry>
		</entries>
	</connection>
	<queue name="exampleQueue">
		<entry name="exampleQueue"></entry>
	</queue>

```

**步骤 5）**启动服务器

**EmbeddedServerDemo.java**

```java
package com.howtodoinjava;

import org.hornetq.core.config.impl.FileConfiguration;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.jms.server.JMSServerManager;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;

public class EmbeddedServerDemo
{
	public static void main(String[] args) throws Exception {
		try
		{
			//Load the file configuration first of all
			FileConfiguration configuration = new FileConfiguration();
			configuration.setConfigurationUrl("hornetq-configuration.xml");
			configuration.start();

			//Create a new instance of hornetq server
			HornetQServer server = HornetQServers.newHornetQServer(configuration);

			//Wrap inside a JMS server
			JMSServerManager jmsServerManager = new JMSServerManagerImpl(
					server, "hornetq-jms.xml");

			// if you want to use JNDI, simple inject a context here or don't
			// call this method and make sure the JNDI parameters are set.
			jmsServerManager.setContext(null);

			//Start the server
			jmsServerManager.start();

			//WOO HOO
			System.out.println("HornetQ server started successfully !!");
		}
		catch (Throwable e)
		{
			System.out.println("Well, you seems to doing something wrong. Please check if config files are in your classes folder.");
			e.printStackTrace();
		}
	}
}

Output in console:

22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: live server is starting..
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: Security risk! It has been detected that the cluster admin user and password have not been changed from the installation default. Please see the HornetQ user guide, cluster chapter, for instructions on how to do this.
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: HornetQ Server version 2.0.0.GA (Hornet Queen, 113) started

HornetQ server started successfully !!

```

[**下载源代码**](https://docs.google.com/file/d/0B7yo2HclmjI4VWhJdWo2UjRhdk0/edit?usp=sharing "hornetq stand alone server tutorial")

**祝您学习愉快！**