大数据实战之环境搭建(十)

本文介绍了Apache ActiveMQ消息队列的基本概念与组件,并通过.NET客户端演示了如何实现消息的发送与接收。涵盖连接工厂、连接、会话、目的地、生产者及消费者的使用。
  1. Html5, 云计算,移动互联网,大数据你知道多少,如果不知道多少,请抓紧时间学习。


今天要说的是消息队列ActiveMQ,这个和cassandra一样也是apache的产品,开源的,支持很多客户端,比如java,C#,ruby,python等。ActiveMQ 是一个完全支持JMS和J2EE规范的 JMS Provider实现。在介绍jms之前,先要介绍一下它的规范,俗话说无规矩不成方圆,就像序列化和反序列化一样,大家要遵循一定的规则才能交互。

JMS的基本构件有

(1).连接工厂, 是客户端用来创建连接的对象,ActiveMQConnectionFactory类。

(2). 连接,JMS Connection封装了客户端和JMS提供者之间的一个虚拟连接,ActiveMQConnectio类。

(3). 会话,JMS Session是生产者和消费者之间Produce message和Consume message的上下文。

会话用于创建消息的生产者,消费者和消息。这个上下文保持了会话的事务性,即发送消息和接受消息被组合到了一个事务中。

(4). 目的地,这个目的地表明了生产消息的目标和消费消息的目标,Destination对象。

JMS规范中定义了两种消息传递域,一种是点对点,一种是发布/订阅。点对点的特点是一个消息只能有一个消费者,消费者和生产者之间没有时间上的相关性,无论消费者在生产者发送消息的时候是否是出于运行状态,他都可以提取到消息。订阅发布模式的特点是一个生产者可以有多个消费者,这种模式存在时间上的相关性,消费者只能消费自从他订阅之后的消息,之前的消息是不能消费的。但是JMS规范允许客户创建持久订阅,即使是在生产者在消费者发送消息的时候处于一个未激活的状态,等他激活以后,依然可以消费未激活之前生产者发送的消息

(5).生产者,是由会话创建的一个对象,它主要职责是将消息发送到目的地

(6).消费者,是由会话创建的一个对象,它主要是接收生产者发送到目的地的消息

消费者可以同步消费和异步消费。


OK,概念就先讲到这里,我们看一下环境

首先从apache 网站上下载ActiveMQ linux版本

211550906.jpg

在这里我下载的版本是5.8.0。OK我们把它放到FTP Server上,在CentOS上拷贝至opt文件夹。

212901178.jpg


ok我们用命令解压

213045265.jpg

解压完成后,直接运行启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@bogon opt] # ls
apache-activemq-5.8.0  apache-activemq-5.8.0-bin. tar .gz
[root@bogon opt] # cd apache-activemq-5.8.0
[root@bogon apache-activemq-5.8.0] # ls
activemq-all-5.8.0.jar  docs     NOTICE           webapps-demo
bin                     example  README.txt       WebConsole-README.txt
conf                    lib      user-guide.html
data                    LICENSE  webapps
[root@bogon apache-activemq-5.8.0] # cd bin
[root@bogon bin] # ls
activemq        activemq.jar  linux-x86-32  macosx
activemq-admin  diag          linux-x86-64  wrapper.jar
[root@bogon bin] # sh activemq

OK,开始启动,到这一步启动成功

213348467.jpg

我们看一下它的管理界面,默认端口是8161,在conf目录下的jetty.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< property  name = "connectors" >
< list >
< bean  id = "Connector"  class = "org.eclipse.jetty.server.nio.SelectChannelConnector" >
< property  name = "port"  value = "8161"  />
</ bean >
<!--
Enable this connector if you wish to use https with web console
-->
<!--
<bean id="SecureConnector" class="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
<property name="port" value="8162" />
<property name="keystore" value="file:${activemq.conf}/broker.ks" />
<property name="password" value="password" />
</bean>
-->
</ list >
</ property >

OK,我们在浏览器中输入Http://localhost:8161/admin,提示输入用户名和密码,用户名:admin,密码admin,

235024493.jpg

这个用户名和密码可以在conf/jetty-realm.properties文件中去修改。

221325400.jpg

OK,管理界面如下

221722632.jpg

因为哥一直做.net开发,所以还是以.net来做个demo,来做个消息的发送可接收。首先需要下载.net客户端。去网站上找到下载.net 客户端的地方

224239392.jpg224342605.jpg

在这里我下载的是1.5.3版本,因为1.6.0版本连接有问题。下载下来之后,我们使用.net 4.0版本

224529297.jpg

OK,新建一个solution,创建两个WinForm的工程,如下

235113326.jpg

注意要引用上面的两个dll,这两个dll分别在build和lib下

235259690.jpg

我们看一下代码,没什么好解释的,先看生产者代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using  Apache.NMS;
using  Apache.NMS.ActiveMQ;
using  Apache.NMS.ActiveMQ.Commands;
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
namespace  ActiveMQSender
{
public  partial  class  FrmMsgSender : Form
{
IConnectionFactory factory;
IConnection connection;
ISession session;
IDestination destination;
IMessageProducer prod;
ITextMessage streamMessage;
~FrmMsgSender()
{
session.Close();
connection.Close();
}
public  FrmMsgSender()
{
InitializeComponent();
this .Init();
}
private  void  Init()
{
factory =  new  ConnectionFactory( "tcp://192.168.192.128:61616/" );
connection = factory.CreateConnection( "admin" "admin" );
session = connection.CreateSession();
destination =  new  ActiveMQTopic( "Bruce Test" );
prod = session.CreateProducer(destination);
streamMessage = prod.CreateTextMessage();
}
private  void  btnSend_Click( object  sender, EventArgs e)
{
streamMessage.Text=txtSendMsg.Text.Trim();
prod.Send(streamMessage,MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
}
}
}

再看消费者代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using  Apache.NMS;
using  Apache.NMS.ActiveMQ;
using  Apache.NMS.ActiveMQ.Commands;
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
namespace  FrmMsgReceiver
{
public  partial  class  FrmMsgReceiver : Form
{
IConnectionFactory factory;
IConnection connection;
ISession session;
IDestination destination;
IMessageConsumer consumer;
~FrmMsgReceiver()
{
session.Close();
connection.Close();
}
public  FrmMsgReceiver()
{
InitializeComponent();
}
private  void  Init()
{
factory =  new  ConnectionFactory( "tcp://192.168.192.128:61616/" );
connection = factory.CreateConnection( "admin" "admin" );
session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
destination =  new  ActiveMQTopic( "Bruce Test" );
consumer = session.CreateConsumer(destination);
}
private  void  FrmMsgReceiver1_Load( object  sender, EventArgs e)
{
this .Init();
IMessage streamMessage = consumer.Receive();
this .txtReceiveMsg.Text = (streamMessage  as  ActiveMQStreamMessage).ReadString();
}
}
}

OK,就是这么简单,我也不写运行过程了,启动VMPlayer后,机器的内存已经使用了90%了,可怜的dell 1420。

002718724.jpg

如果大家有兴趣的话,看一下这篇文章



本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/1263167,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值