Handle-Message的发送和处理

1、Handle,MessageQueue,Message类图

Handle: 处理消息,并提供一系列函数帮忙我们创建消息和插入消息到消息队列中

创建handle实例--PbapClientConnectionHandler

//创建一个线程

         mHandlerThread =
                    new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND);
            mHandlerThread.start();

//将这个线程设置为消息处理Looper线程
            mConnectionHandler =
                    new PbapClientConnectionHandler.Builder().setLooper(mHandlerThread.getLooper())
                            .setContext(mService)
                            .setClientSM(PbapClientStateMachine.this)
                            .setRemoteDevice(mCurrentDevice)
                            .build();

 

Looper作用:Looper的prepare函数将Looper和调用prepare的线程绑定在一起,调用线程调用loop函数处理来自该消息队列的消息。

Android 系统的消息队列和消息循环都是针对具体线程的,一个线程可以存在(当然也可以不存在)一个消息队列和一个消息循环(Looper),特定线程的消息只能分发给本线程,不能进行跨线程通讯。但是创建的工作线程默认是没有消息循环和消息队列的,如果想让该线程具有消息队列和消息循环,需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环

MessageQueue:消息队列,Handle和Looper中使用的是同一个消息队列

 

 

2、发送消息

 

3、处理消息

looper处理消息:

loop 使消息循环起作用,取消息,处理消息

    /**

     * Run the message queue in this thread. Be sure to call

     * {@link #quit()} to end the loop.

     */

    public static void loop() {

        final Looper me = myLooper();//返回保存在调用线程TLV中的Looper对象

        if (me == null) {

            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

        }

        final MessageQueue queue = me.mQueue;//取得Looper对象的消息队列

        // Make sure the identity of this thread is that of the local process,

        // and keep track of what that identity token actually is.

        Binder.clearCallingIdentity();

        final long ident = Binder.clearCallingIdentity();

        for (;;) {

            Message msg = queue.next(); // might block 取消息队列中的一个待处理消息

            if (msg == null) {

                // No message indicates that the message queue is quitting.

                return;

            }

            // This must be in a local variable, in case a UI event sets the logger

            Printer logging = me.mLogging;

            if (logging != null) {

                logging.println(">>>>> Dispatching to " + msg.target + " " +

                        msg.callback + ": " + msg.what);

            }

            msg.target.dispatchMessage(msg);//调用该消息的Handle,交给它的dispatchMessage函数处理

Handle -dispatchMessage

 /**
     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {

//Message的callback不为空,则直接调用Message的callback来处理消息
            handleCallback(msg);
        } else {
            if (mCallback != null) {

//Handle的全局Callback不为空
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }

//调用handle子类的handleMessage来处理消息
            handleMessage(msg);
        }
    }

Message.callback用法:将Runnable当做一个Message

Runnable线程处理使用实例

    mHandler.post(new Runnable() {

                    @Override

                    public void run() {

                        final IBinder b = callbacks.asBinder();

 

                });

        }

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,IntegrationFlows确实可以接收MessageHandler和ServiceBusTemplate。 在Spring Integration中,我们可以使用MessageHandler将消息发送到Azure Service Bus队列或主题。使用MessageHandler的好处是,它隐藏了与Azure Service Bus交互的细节,并提供了更高级别的抽象,从而使发送消息变得更加简单和方便。 下面是一个使用MessageHandler将消息发送到Azure Service Bus的示例: ```java @Bean public IntegrationFlow messageFlow(ServiceBusTemplate serviceBusTemplate) { return IntegrationFlows.from(() -> MessageBuilder.withPayload("Hello, World!").build(), e -> e.poller(p -> p.fixedDelay(1000))) .handle(AzureServiceBus.outboundAdapter(serviceBusTemplate).destination("my-queue")) .get(); } ``` 在这个例子中,我们使用AzureServiceBus.outboundAdapter()方法创建了一个Azure Service Bus的输出适配器,并将其添加到IntegrationFlow中。我们还使用destination()方法指定了要发送到的队列名称,这里是"my-queue"。 另一方面,ServiceBusTemplate是Azure Service Bus的Java客户端库的一部分,可以用于发送和接收消息,管理队列和主题等操作。我们可以将ServiceBusTemplate注入到Spring Integration的组件中,并在需要的时候使用它来发送或接收消息。 下面是一个使用ServiceBusTemplate将消息发送到Azure Service Bus的示例: ```java @Bean public IntegrationFlow messageFlow(ServiceBusTemplate serviceBusTemplate) { return IntegrationFlows.from(() -> MessageBuilder.withPayload("Hello, World!").build(), e -> e.poller(p -> p.fixedDelay(1000))) .handle(m -> { serviceBusTemplate.send("my-queue", m); System.out.println("Message sent: " + m.getPayload()); }) .get(); } ``` 在这个例子中,我们在IntegrationFlow中添加了一个处理器来处理消息。在处理器中,我们使用ServiceBusTemplate的send()方法将消息发送到"my-queue"队列,并在控制台输出消息内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值