Java 回调函数Callback


参考:http://blog.csdn.net/fengyifei11228/article/details/5729445

           http://kidult.iteye.com/blog/148982


所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数。一般说来,C不会自己调用B,C提供B的目的就是让S来调用它,而且是C不得不提供。由于S并不知道C提供的B姓甚名谁,所以S会约定B的接口规范(函数原型),然后由C提前通过S的一个函数R告诉S自己将要使用B函数,这个过程称为回调函数的注册,R称为注册函数。Web Service以及Java的RMI都用到回调机制,可以访问远程服务器程序。


下面举个通俗的例子:
某天,我打电话向你请教问题,当然是个难题,你一时想不出解决方法,我又不能拿着电话在那里傻等,于是我们约定:等你想出办法后打手机通知我,这样,我就挂掉电话办其它事情去了。过了XX分钟,我的手机响了,你兴高采烈的说问题已经搞定,应该如此这般处理。故事到此结束。这个例子说明了“异步+回调”的编程模式。其中,你后来打手机告诉我结果便是一个“回调”过程;我的手机号码必须在以前告诉你,这便是注册回调函数;我的手机号码应该有效并且手机能够接收到你的呼叫,这是回调函数必须符合接口规范。


下面是一个回调的实例>>

客户端具体业务类:

public class ClientBusiness
{
    /**
     * 客户端具体业务
     * @see
     */
    public static void dispose()
    {
        System.out.println("dispose with Client Business……");
        
        //anything business else
    }

}

客户端提供供服务端回调的接口:

/**
 * 回调接口
 * @author will_awoke
 * @version 2014-6-17
 * @see ClientCallBack
 * @since
 */
public interface ClientCallBack
{
    
    /**
     * 回调接口的实际执行方法 
     * @see
     */
    void execute();
    
}

服务端的回调处理:

public class ServerRemote
{

    /**
     * 暴漏给Client调用的远程方法
     * @see
     */
    public  void remote()
    {
        //满足一定的业务条件,则调用回调函数,在回调函数内执行客户端的具体业务。
        if (someCondition())
        {
            call(new ClientCallBack()
            {
                @Override
                public void execute()
                {
                    ClientBusiness.dispose();
                }
            });
        }
    }
    
    private boolean someCondition()
    {
        //满足一定的业务条件 some code else ……
        return true;
    }

    private void call(ClientCallBack callBack)
    {
        //回调函数执行
        callBack.execute();
    }

}

客户端远程调用:

public class ClientRmi
{

    /**  
     * 客户端调用服务端远程方法
     * @param args 
     * @see 
     */
    public static void main(String[] args)
    {
        ServerRemote sr = new ServerRemote();
        sr.remote();
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Features Automatic mapping from Java to native functions, with simple mappings for all primitive data types Runs on most platforms which support Java Automatic conversion between C and Java strings, with customizable encoding/decoding Structure and Union arguments/return values, by reference and by value Function Pointers, (callbacks from native code to Java) as arguments and/or members of a struct Auto-generated Java proxies for native function pointers By-reference (pointer-to-type) arguments Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate) Demo applications/examples Supported on 1.4 or later JVMs, including JavaME (earlier VMs may work with stubbed NIO support) Customizable marshalling/unmarshalling (argument and return value conversions) Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros Support for automatic Windows ASCII/UNICODE function mappings Varargs support Type-safety for native pointers VM crash protection (optional) Optimized direct mapping for high-performance applications. COM support for early and late binding. COM/Typelib java code generator. Community and Support All questions should be posted to the jna-users Google group. Issues can be submitted here on Github. When posting to the mailing list, please include the following: What OS/CPU/architecture you're using (e.g. Windows 7 64-bit) Reference to your native interface definitions (i.e. C headers), if available The JNA mapping you're trying to use VM crash logs, if any Example native usage, and your attempted Java usage It's nearly impossible to indicate proper Java usage when there's no native reference to work from. For commercial support, please contact twalljava [at] java [dot] net. Using the Library Getting Started Functional Description. Mapping between Java and Native Using Pointers and Arrays Using Structures and Unions Using By-Reference Arguments Customization of Type Mapping Callbacks/Function Pointers/Closures Dynamically Typed Languages (JRuby/Jython) Platform Library Direct Method Mapping (Optimization) Frequently Asked Questions (FAQ) Avoiding Crashes Primary Documentation (JavaDoc) The definitive JNA reference is in the JavaDoc. Developers Contributing to JNA Setting up a Windows Development Environment Setting up an Android Development Environment Setting up a RaspberryPi Development Environment Setting up a Mac Development Environment Releasing JNA Publishing to Maven Central Contributing You're encouraged to contribute to JNA. Fork the code from https://github.com/java-native-access/jna and submit pull requests. For more information on setting up a development environment see Contributing to JNA. If you are interested in paid support, feel free to say so on the jna-users mailing list. Most simple questions will be answered on the list, but more complicated work, new features or target platforms can be negotiated with any of the JNA developers (this is how several of JNA's features came into being). You may even encounter other users with the same need and be able to cost share the new development. License This library is licensed under the LGPL, version 2.1 or later, and (from version 4.0 onward) the Apache Software License, version 2.0. Commercial license arrangements are negotiable. NOTE: Oracle is not sponsoring this project, even though the package name (com.sun.jna) might imply otherwise.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值