如何从ColdFusion实现Java接口? [英] How can I implement a Java interface from ColdFusion?

查看:154
本文介绍了如何从ColdFusion实现Java接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ColdFusion应用程序通过Apple的APNS服务发送推送通知。我正在使用 notnoop apns java库。我已经成功发送推送通知使用这个,但是,最近遇到了一些问题。我想使用提供的ApnsDelegate接口,以帮助调试这个问题,但是,我不知道如何在ColdFusion中实现一个Java接口。我不是Java程序员。请帮助。

I am working on a ColdFusion app to send Push Notifications via Apple's APNS service. I am using the notnoop apns java library. I have successfully sent push notifications using this, but, recently have run into some issues. I want to use the ApnsDelegate Interface that is provided in order to help debug the issue, but, I do not know how to implement a Java interface in ColdFusion. I am not a Java programmer. Please help.

更新:到目前为止,我已经写了一个Java类来实现该接口,但是,我不知道如何 ColdFusion。我尝试在java方法(使用log4j)内部日志,但是这也不工作。

Update: So far, I've actually written a Java class to implement the interface, but, I cannot figure out how to "bubble" the events to ColdFusion. I've tried logging within the java methods (using log4j), but that's not working either.

我真的只需要一个方法来捕获这些java方法被调用时,

I really just need a way to capture when these java methods are called, and to log some info from the arguments being passed.

推荐答案

(从注释展开)

对于CF8,可以使用JavaLoader的 CFCDynamicProxy 。这是一个奇妙的功能, Mark Mandel 一会儿就添加了。本质上,它允许您使用CFC,就像它是一个具体的java类。注意,它只适用于接口。 (CF10 +包含JavaLoader的rip,所以代理功能被烘焙。参见创建动态代理示例。)。

For CF8, it can be done with the JavaLoader's CFCDynamicProxy. It is a fantastic feature Mark Mandel added a while back. Essentially it lets you use a CFC, as if it were a concrete java class. Note, it only applies to interfaces. (CF10+ contains a rip of the JavaLoader, so the proxy feature is baked in. See Creating a Dynamic Proxy example.).

要使用动态代理,只需创建一个CFC,实现定义的所有方法接口。重要的是,函数签名与接口(类型,访问等)中的方法匹配。查看 API ,类似这样的操作应该有效。

To use the dynamic proxy, just create a CFC that implements all of the methods defined in the interface. It is important that the function signatures match the methods in the interface (types, access, etcetera). Looking over the API, something like this should work.

<cfcomponent>
    <!--- Note: If the argument is a java class, use type="any" --->
    <cffunction name="connectionClosed" returntype="void" access="package">
        <cfargument name="DeliveryError" type="any" required="true" />
        <cfargument name="MessageIdentifier" type="numeric" required="true" />

        <!--- do stuff here --->       
    </cffunction>   

    <cffunction name="messageSendFailed" returntype="void" access="package"
            hint="Called when the delivery of the message failed for any reason">
        <cfargument name="message" type="any" required="true" />
        <cfargument name="failedError" type="any" required="true" />

        <!--- do stuff here --->       
    </cffunction>   

    <cffunction name="messageSent" returntype="void" access="package"
            hint="Called when message was successfully sent to the Apple servers">
        <cfargument name="message" type="any" required="true" />

        <!--- do stuff here --->       
    </cffunction>   
</cfcomponent>

然后使用动态代理创建它的实例。然后,您可以在任何期望使用 ApnsDelegate 对象的地方使用该实例,就像它是您在java中写的一个具体类。

Then use the dynamic proxy to create an instance of it. You can then use the instance anywhere that expects an ApnsDelegate object, just as if it were a concrete class you wrote in java.

<cfscript>

   // add the paths of required jars into an array
   paths = [ expandPath("/path/to/cfcdynamicproxy.jar")
            , expandPath("/path/to/the_apns.jar")
           ];

   // MUST load the CF class path in order to use the proxy
   loader = createObject("component", "javaLoader.JavaLoader").init(   
                              loadPaths=paths
                             , loadColdFusionClassPath=true
                       );

   // store "names" of all interfaces the proxy implements
   interfaces = [ "com.notnoop.apns.ApnsDelegate" ];

   // grab reference to proxy class
   proxy = loader.create("com.compoundtheory.coldfusion.cfc.CFCDynamicProxy");

   // finally create the delegate
   delegate = proxy.createInstance( "c:/path/to/YourComponent.cfc"
                          , interfaces );

   // debugging
   writeDump( delegate );

   // .. now pass the delegate into some other java method 
</cfscript>

这篇关于如何从ColdFusion实现Java接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆