我正在开发一个 ColdFusion 应用程序以通过 Apple 的 APNS 服务发送推送通知。我正在使用 notnoop apns java library .我已经使用它成功发送了推送通知,但是最近遇到了一些问题。我想使用提供的 ApnsDelegate 接口(interface)来帮助调试问题,但是,我不知道如何在 ColdFusion 中实现 Java 接口(interface)。我不是 Java 程序员。请帮忙。
更新:到目前为止,我实际上已经编写了一个 Java 类来实现接口(interface),但是,我不知道如何将事件“冒泡”到 ColdFusion。我已经尝试在 java 方法中进行日志记录(使用 log4j),但这也不起作用。
我真的只需要一种方法来捕获这些 java 方法何时被调用,并从传递的参数中记录一些信息。
最佳答案
(从评论中扩展)
对于 CF8,可以使用 JavaLoader 的 CFCDynamicProxy 来完成。 .这是一个很棒的功能 Mark Mandel不久前补充。本质上,它允许您使用 CFC,就好像它是一个具体的 Java 类一样。请注意,它仅适用于接口(interface)。 (CF10+ 包含 JavaLoader 的一个 rip,因此代理功能被内置。参见 Creating a Dynamic Proxy 示例。)
要使用动态代理,只需创建一个实现接口(interface)中定义的所有方法的 CFC。函数签名与接口(interface)中的方法(类型、访问等)匹配很重要。翻看API , 这样的事情应该有效。
<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 编写的具体类一样。
<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>
关于java - 如何从 ColdFusion 实现 Java 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244067/