---
title: "安装配置ActiveMQ5.0"
author: "loving863"
date: "2008-08-27T15:09:26Z"
tags: ["ActiveMQ", "Apache", "JMS", "Unix", "应用服务器"]
slug: "activemq5-0"
draft: false
lastmod: "2008-08-27T15:09:26Z"
id: 233206
url: "https://jinguo.iteye.com/blog/233206"
---

### 简介

  ActiveMQ 是开源的JMS实现，Geronimo应用服务器就是使用的ActiveMQ提供JMS服务。ActiveMQ5.0相比以前版本提供了一些非常有用的新功能：

1. AMQ Message Store (Faster Persistence!)
2. Cursors (To handle very large number of stored messages)
3. Blob Messages
4. Command Agent
5. Enterprise Integration Patterns via Camel Integration
6. Logging a warning if you forget to start a Connection
7. Message Transformation
8. Mirrored Queues
9. Flow Control

鉴于目前关于ActiveMQ5.0的文章比较少，故准备写一系列ActiveMQ的使用方面的文章。本篇先从安装开始。 

### 安装

1. 在[http://activemq.apache.org/download.html](http://activemq.apache.org/download.html)下载5.0.0发行包，解压到需要安装ActiveMQ的文件夹，记为/path/to/activemq。
2. unix环境activemq文件夹需要执行权限，执行如下命令  chmod -R 755 /path/to/activemq

### 启动

1. window环境运行/path/to/activemq/bin/activemq.bat
2. unix环境运行/path/to/activemq/bin/activemq

### 测试

ActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否成功启动

1. window环境运行  netstat -an|find "61616"
2. unix环境运行netstat -an|grep 61616

### 监控

ActiveMQ5.0版本默认启动时，启动了内置的jetty服务器，提供一个demo应用和用于监控ActiveMQ的admin应用。

admin：[http://127.0.0.1:8161/admin/](http://127.0.0.1:8161/admin/)

demo：[http://127.0.0.1:8161/demo/](http://127.0.0.1:8161/demo/)

 

点击demo应用中的“ [Market data publisher](http://127.0.0.1:8161/demo/portfolioPublish?count=1&amp%3Brefresh=2&amp%3Bstocks=IBMW&amp%3Bstocks=BEAS&amp%3Bstocks=MSFT&amp%3Bstocks=SUNW)”，就会发一些测试的消息。转到admin页面的topics menu下面（queue和topic的区别见 [http://andyao.iteye.com/blog/153173](../../../blog/153173)），可以看到消息在增长。

![](../../../upload/picture/pic/6695/5420ab30-72c6-3b30-9492-388a5a0e5e66.gif "点击查看原始大小图片")

### 配置

ActiveMQ5.0的配置文件在/path/to/activemq/conf目录下面。主要配置文件为activemq.xml，具体的配置将在后续文章中详细说明。

 

－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

 

 

 

持久化时不能对自定义类型的对象进行保存，看是不是你的messageConverter有问题？你的消息类型是POJO吗？

 

 

MessageConverter如下

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/153171 "复制代码")

01. **public** **class** DefaultMessageConverter **implements** MessageConverter {
02. /\**
03. * Logger for this class
04. \*/
05. **private** **static** **final** Log log = LogFactory.getLog(DefaultMessageConverter.**class**);
06. **public** Message toMessage(Object obj, Session session) **throws** JMSException {
07. **if** (log.isDebugEnabled()) {
08. log.debug("toMessage(Object, Session) - start");
09. }
10. // check Type
11. ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
12. HashMap&lt;String, **byte**\[]&gt; map = **new** HashMap&lt;String, **byte**\[]&gt;();
13. **try** {
14. // POJO must implements Seralizable
15. ByteArrayOutputStream bos = **new** ByteArrayOutputStream();
16. ObjectOutputStream oos = **new** ObjectOutputStream(bos);
17. oos.writeObject(obj);
18. map.put("POJO", bos.toByteArray());
19. objMsg.setObjectProperty("Map", map);
20. } **catch** (IOException e) {
21. log.error("toMessage(Object, Session)", e);
22. }
23. **return** objMsg;
24. }
25. **public** Object fromMessage(Message msg) **throws** JMSException {
26. **if** (log.isDebugEnabled()) {
27. log.debug("fromMessage(Message) - start");
28. }
29. **if** (msg **instanceof** ObjectMessage) {
30. HashMap&lt;String, **byte**\[]&gt; map = (HashMap&lt;String, **byte**\[]&gt;) ((ObjectMessage) msg).getObjectProperty("Map");
31. **try** {
32. // POJO must implements Seralizable
33. ByteArrayInputStream bis = **new** ByteArrayInputStream(map.get("POJO"));
34. ObjectInputStream ois = **new** ObjectInputStream(bis);
35. Object returnObject = ois.readObject();
36. **return** returnObject;
37. } **catch** (IOException e) {
38. log.error("fromMessage(Message)", e);
39. } **catch** (ClassNotFoundException e) {
40. log.error("fromMessage(Message)", e);
41. }
42. **return** **null**;
43. } **else** {
44. **throw** **new** JMSException("Msg:\[" + msg + "] is not Map");
45. }
46. }
47. }