ActiveMQ knowledge

Availability message queue

持久化, 事务, 签收, zookeeper+replicated-leveldb-storeMaster-slave cluster

Asynchronous Transmission

Sync Transmit:

  1. Explicitly specify synchronous transmission
  2. Under the premise of not using transactions, sending persistent messages (synchronous transmission will be used, and the producer will block until the broker returns an acknowledgment indicating that the message has been persistent. Delay will bring obstruction clients)

Asynchronous Transmission: improve transmission efficiency, but will increase the consumption of broker performance, and can not effectively ensure that messages sent successfully.

Open the way http://activemq.apache.org/async-sends

ActiveMQ sent using asynchronous default: non-persistent messages, messages within a transaction are transmitted asynchronously; for persistent message, the synchronous transmission.

If specified useAsyncSend=true, it will be transmitted asynchronously, including PERSISTENTthe type of message, also transmitted using asynchronous. In useAsyncSendthe case of the client 需要容忍消息丢失的可能.

Right Asynchronous transmission method: receive callbacks
on the receipt by the client to determine again whether sent successfully

        ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        TextMessage message = null;
        for (int i = 0; i < count; i++) {
            message = session.createTextMessage("Hello-"+i)
            message.setJMSmessageID(UUID.randomUUID().toString());
            String msgID = message.getJMSmessageID();
            producer.send(message , new AsyncCallback() {
                @Override
                public void onSuccess() {
                    System.out.println(msgID+"成功");
                }

                @Override
                public void onException(JMSException exception) {
                    System.out.println(msgID+"失败");
                    exception.printStackTrace();
                }
            });
        }
1. 同步调用
    是指从请求的发起一直到最终的处理完成期间,请求的调用方一直在同步阻塞等待调用的处理完成。

ClientCode在send发出请求后,就一直都阻塞在这里,调用Service以后,Service会调用Adapter这样一个类来进行处理,
而这个类会调用远程的一个服务等待最终调用结果的返回,是成功还是失败。
因为这个过程是阻塞等待的,所以这个过程也就是同步调用。

2.异步调用
    是指在请求发起的处理过程中,客户端的代码已经返回了,
    它可以继续进行自己的后续操作,而不需要等待调用处理完成,这就叫做异步调用。

异步调用过程,用户Clientcode在send发出请求后,调用Service以后,Service会把这个调用请求发送给消息队列,然后就立即返回了。
Clientcode收到返回以后继续向下处理,不会继续阻塞等待。
在这个过程中,客户端的调用,也就是应用程序的调用,和业务逻辑真正发送邮件的操作是不同步的。

Guess you like

Origin www.cnblogs.com/loveer/p/11408513.html