MQ NIO

ApacheMINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可靠性的网络应用程序。它提供了一个通过JavaNIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步API。

  Apache MINA 也称为:

  ● NIO 框架库

  ●客户端服务器框架库

  ● 一个网络套接字库

  MINA虽然简单但是仍然提供了全功能的网络应用程序框架:

  ● 为不同的传输类型提供了统一的API:

  ○ 通过JavaNIO提供TCP/IP 和 UDP/IP支持

  ○ 通过RXTX提供串口通讯(RS232)

  ○ In-VM管道通讯

  ○ 你能实现你自己的API!

  ● 过滤器作为一个扩展特性;类似Servlet过滤器

  ● 低级和高级的API:

  ○ 低级:使用字节缓存(ByteBuffers)

  ○ 高级:使用用户定义的消息对象(objects)和编码(codecs)

  ● 高度定制化线程模型:

  ○ 单线程

  ○ 一个线程池

  ○ 一个以上的线程池(也就是SEDA)

  ● 使用Java5 SSL引擎提供沙盒(Out-of-the-box) SSL ·TLS · StartTLS支持

  ● 超载保护和传输流量控制

  ● 利用模拟对象进行单元测试

  ● JMX管理能力

  ● 通过StreamIoHandler提供基于流的I/O支持

  ● 和知名的容器(例如PicoContainer、Spring)集成

  ●从Netty平滑的迁移到MINA,Netty是MINA的前辈。

MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们。消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ。

目录

1.MQ的特点(opengoss)

2. 使用场景

3.RabbitMQ是什么

4.RabbitMQ安装

5. 编写RabbitMQ生产者客户端

6. 编写消费者客户端

7.RabbitMQ的几个概念

8.RabbitMQ消息持久化

编辑本段1. MQ的特点(opengoss)

  MQ的消费-生产者模型的一个典型的代表,一端往消息队列中不断的写入消息,而另一端则可以读取或者订阅队列中的消息。MQ和JMS类似,但不同的是JMS是SUN JAVA消息中间件服务的一个标准和API定义,而MQ则是遵循了AMQP协议的具体实现和产品。

编辑本段2. 使用场景

  最近在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了服务器的请求响应时间,从而提高了系统的吞吐量。

编辑本段3. RabbitMQ是什么

  rabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla PublicLicense开源协议。

编辑本段4. RabbitMQ安装

  4.1)安装ERLANG

  首先,因为RabbitMQ由ERLANG实现,下载ERLANG 源代码。

  解压源代码至ERLANG至文件夹$ERLANG

  安装依赖包:

  Yum install tk

  Yum install tcl

  Yum installunixODBC

  进入$ERLANG.编译ERLANG

  ./configure–prefix=/usr/local/erlang

  ./make

  ./make install

  并将erlang bin目录加至PATH

  4.2)安装rabbitMQ

  下载RabbitMQ ,解压至$RMQ。

  启动RabbitMQ

  ./bin/rabbitmq-server

编辑本段5. 编写RabbitMQ生产者客户端

  publicclassMQTestor {

  publicstaticvoidmain(String[]args)throwsException {

  ConnectionParametersparams =newConnectionParameters();

  params.setUsername("guest");

  params.setPassword("guest");

  params.setVirtualHost("/");

  params.setRequestedHeartbeat(0);

  ConnectionFactoryfactory =newConnectionFactory(params);

  Connection conn =factory.newConnection("192.168.1.101", 5672);

  Channel channel =conn.createChannel();

  channel.exchangeDeclare("ex1","direct",true);

  channel.queueDeclare("q1",true);

  channel.queueBind("q1","ex1", "m1");

  byte[] msg = "helloworld".getBytes();

  channel.basicPublish("ex1","m1", MessageProperties.PERSISTENT_TEXT_PLAIN, msg);

  channel.close();

  conn.close();

  }

  }

编辑本段6. 编写消费者客户端

  ConnectionParametersparams =newConnectionParameters();

  params.setUsername("guest");

  params.setPassword("guest");

  params.setVirtualHost("/");

  params.setRequestedHeartbeat(0);

  ConnectionFactoryfactory =newConnectionFactory(params);

  Connection conn =factory.newConnection("192.168.1.101", 5672);

  Channel channel =conn.createChannel();

  GetResponseres=channel.basicGet("q1",false);

  if(res!=null){

  System.out.println(newString(res.getBody()));

  channel.basicAck(res.getEnvelope().getDeliveryTag(),false);

  }else{

  System.out.println("Nomessage!");

  }

编辑本段7. RabbitMQ的几个概念

  Exchange:交换机,决定了消息路由规则;

  Queue:消息队列;

  Channel:进行消息读写的通道;

  Bind:绑定了Queue和Exchange,意即为符合什么样路由规则的消息,将会放置入哪一个消息队列;

编辑本段8. RabbitMQ消息持久化

  1) 将交换机置为可持久;

  2) 将通道置为可持久

  3) 消息发送时设置可持久。

  当我们“生产”了一条可持久化的消息,尝试中断MQ服务,启动消费者获取消息,消息依然能够恢复。相反,则抛出异常

What messaging scenariosare supported by AMQP and RabbitMQ?

AMQP is a very generalsystem that can be configured to cover a great variety of messaging middlewareuse-cases. For example:

·Point-to-point communication

Oneof the simplest and most common scenarios is for a message producer to transmita message addressed to a particular message consumer. AMQP covers this scenarioby allowing queues to be named and to be bound to a "direct"exchange, which routes messages to queues by name.

·One-to-many broadcasting(including multicast)

Inthis scenario, the broadcasters publish messages to an AMQP "fanout"exchange, and subscribers create and subscribe to their own private AMQPqueues, which forward published messages on to them, with one copy per queue.

Multicastis addressed at the broker implementation level. AMQP clients need not be madeaware of transport-level optimisations such as multicast: broker clusters arefree to use whatever such low-level optimisations are available fromconfiguration to configuration.

Multipleoptimisations are possible, since AMQP separates routing logic (exchanges andbindings) from message queueing (queues). Multicast relates only to routingfrom message publishers to message queues, and as a routing optimisation can becompletely physically decoupled from AMQP's logical semantics. Furtheroptimisations include physical separation of exchange from queue or evencolocation of queue with a consumer application.

·Transactional publication andacknowledgement

AMQPsupports transactional publication, where an AMQP channel is opened,transactional mode is selected, messages are published and acknowledged, andthe transaction is committed. The system guarantees atomicity and durabilityproperties for transactional message activity.

·High-speed transient messageflows

Messagesare individually flagged as transient or persistent in AMQP at the time ofpublication. By sending messages outside the transactional part of theprotocol, in non-persistent mode, an application can achieve very highthroughput and low latency.

·Reliable persistent message delivery

Messagesthat are published in persistent mode are logged to disk for durability. If theserver is restarted, the system ensures that received persistent messages arenot lost. The transactional part of the protocol provides the final piece ofthe puzzle, by allowing the server to communicate its definite receipt of a setof published messages.

·Store-and-forward

Store-and-forwardis implemented by delivering messages marked as "persistent" toAMQP's durable queues. Published, persistent messages delivered to durablequeues are stored on disk until a consumer retrieves and deletes them.

·Wide area messaging

Becauserouting logic is decoupled from message delivery, RabbitMQ is able to supportextended broker clustering across WANs. Some of the approaches includeAJAX-style access to AMQP resources, and spanning-tree pseudo-multicastimplemented internally to a RabbitMQ cluster.

·File streaming

TheAMQP protocol, version 0-8, supports file streaming by way of thede>filede> content class. Verylarge files are transferred to a temporary area on the broker before beingrouted to queues for download by consumers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值