AMF (Action Message Format) Calls can be triggered from Java Client as well. AMFConnection class can be used for the same. Documentation is available at: http://livedocs.adobe.com/blazeds/1/javadoc/flex/messaging/io/amf/client/AMFConnection.html
Below is a simple example usage:
- Setup BlazeDS and create simple destination as explained by Sujit Reddy on his blog. URL: http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/
- Create following two JSP files – ‘index.jsp’, ‘callAMF.jsp’. Copy these files on server. In my case, application name was ‘JavaAMFClientExample’. I copied files to root of ‘JavaAMFClientExample’ application on server.
- Create Java Class ‘JavaAMFClientImpl’; add BlazeDS JAR files to Java project. Copy class file in appropriate directory on server. In my case path of class file was – <TomcatServer\webapps>\JavaAMFClientExample\WEB-INF\classes\com\tushar\JavaAMFClientExample\JavaAMFClientImpl.class
- Run index.jsp and click ‘Call Service’ button. This will make an AJAX call to ‘callAMF.jsp’ and will display server response in same page.
index.jsp:
<HTML>
<head>
<style type="text/css">
table {
border-width: 0px;
border-spacing: 2px;
border-style: dashed;
border-color: gray;
border-collapse: separate;
background-color: white;
}
table td {
border-width: 0px;
padding: 0px;
border-style: inset;
border-color: gray;
background-color: white;
-moz-border-radius: 0px 0px 0px 0px;
}
p, li, td { font-family: verdana; font-size: 12px }
</style>
<script type="text/javascript">
{
var xmlHttp;
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) { // Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4) {
document.getElementById("response").innerHTML = xmlHttp.responseText;
}
}
function onFormSubmit()
{
var destination = document.getElementById("destination");
var method = document.getElementById("method");
var name = document.getElementById("name");
if(destination != null){
destination = destination.value
}
if(method != null){
method = method.value
}
if(name != null){
name = name.value
}
var params = "destination="+destination+"&method="+method+"&name="+name+"&sid="+Math.random();
var url="callAMF.jsp?"+params;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
}
</script>
</head>
<BODY>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="1" width="100">
<tr>
<td colspan="2" align="center"><div><h5>Test AMF Call</h1></div></td>
</tr>
<tr>
<td>Destination:</td>
<td><INPUT TYPE=TEXT id="destination" value="CreatingRpc" SIZE=20></td>
</tr>
<tr>
<td>Method:</td>
<td><INPUT TYPE=TEXT id="method" value="getResults" SIZE=20></td>
</tr>
<tr>
<td>Name:</td>
<td><INPUT TYPE=TEXT id="name" value="Tushar" SIZE=20></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Call Service" onclick="onFormSubmit()" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left"><div id="response"></td>
</tr>
</BODY>
</HTML>
callAMF.jsp:
<%@ page language="java" %>
<%@ page import="com.tushar.JavaAMFClientExample.*" %>
<%
String destination=request.getParameter("destination");
String method=request.getParameter("method");
String name=request.getParameter("name");
JavaAMFClientImpl javaAMFClientService = new JavaAMFClientImpl();
Object obj = javaAMFClientService.sendAMFMessage(destination, method, name);
%>
<p>Message from Server: <%=obj%><p>
JavaAMFClientImpl.java
package com.tushar.JavaAMFClientExample;
import flex.messaging.io.amf.client.AMFConnection;
import flex.messaging.io.amf.client.exceptions.ClientStatusException;
import flex.messaging.io.amf.client.exceptions.ServerStatusException;
public class JavaAMFClientImpl {
public Object sendAMFMessage(String destination, String method, String name) throws ClientStatusException, ServerStatusException {
// Create the AMF connection.
AMFConnection amfConnection = new AMFConnection();
// Connect to the remote url.
String url = "http://localhost:8080/JavaAMFClientExample/messagebroker/amf";
try
{
amfConnection.connect(url);
}
catch (ClientStatusException cse)
{
System.out.println(cse);
return null;
}
// Make a remoting call and retrieve the result.
try
{
Object result = amfConnection.call(destination+"."+method, name);
return result;
}
catch (ClientStatusException cse)
{
System.out.println(cse);
return null;
}
catch (ServerStatusException sse)
{
System.out.println(sse);
return null;
}
finally {
amfConnection.close();
}
}
}
Java Project:
Flex Client:
JSP Client:
JavaScript AMF Implementation:
James Ward has created a Pure JavaScript AMF implementation. Here are more details: http://css.dzone.com/news/amfjs-%E2%80%93-pure-javascript-amf
Advertisement







Hi, Tushar, I read this blog. how great. it is good.
Thanks,
Raghunath D. Sontakke
By: Raghunath D. Sontakke on December 24, 2010
at 10:25 am
Hi, Tushar I read this blog. it is good.
Raghunath Sontakke
By: Raghunath Sontakke on December 24, 2010
at 10:27 am
Why have the intermediate jsp that translates to AMF? Why not use AMF directly in javascript?
http://www.jamesward.com/2010/07/07/amf-js-a-pure-javascript-amf-implementation/
By: Dusty on January 20, 2011
at 4:21 am
Yes, you can. BTW current JS implementation has some limitations.
By: Tushar on January 20, 2011
at 6:04 am
James Ward’s Javascript AMF implementation is only AMF deserialization, i.e. it only works for server-to-client updates but not client-to-server updates.
By: Mete Atamel on January 20, 2011
at 8:25 am
Thanks for a nice article.
By: Sandeep on February 11, 2011
at 8:55 am