in this post i have tried integrating flex and java using Blazeds. Instead of a simple hello world program , i tried the mail functionality.
Flex can communicate with the server(Java) using remote objects as well. Using this methodology BlazeDs makes the communication with server(Java) very simple.
Blazeds is available in the net. And there are many samples as well. I will explain some of the import configuration.
I have attached a zip file ,which you can unzip and deploy it in tomcat. But you may need to change some properties in the java source and compile . from address, smtp host and port.
please download the FlexMail.zip file from the below location.
Java code ::
package testblaze;
/**
*
* @author Raghuram
*/
import javax.mail.*;
import javax.mail.internet.*;
public class FlexMail {
public String sendMail(String to, String subject,
String content){
try {
System.out.println("content--"+content);
send(to, subject, content);
return "Mail Sent Successfully";
} catch (AddressException ex) {
// Logger.getLogger(FlexMail.class.getName()).log(Level.SEVERE, null, ex);
return ex.toString();
} catch (MessagingException ex) {
// Logger.getLogger(FlexMail.class.getName()).log(Level.SEVERE, null, ex);
return ex.toString();
}
}
public void send(String to, String subject,
String content) throws AddressException, MessagingException {
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp host");
props.put("mail.smtp.port", "" + "smtp port");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("from address"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);
Transport.send(msg);
}
}
/**
*
* @author Raghuram
*/
import javax.mail.*;
import javax.mail.internet.*;
public class FlexMail {
public String sendMail(String to, String subject,
String content){
try {
System.out.println("content--"+content);
send(to, subject, content);
return "Mail Sent Successfully";
} catch (AddressException ex) {
// Logger.getLogger(FlexMail.class.getName()).log(Level.SEVERE, null, ex);
return ex.toString();
} catch (MessagingException ex) {
// Logger.getLogger(FlexMail.class.getName()).log(Level.SEVERE, null, ex);
return ex.toString();
}
}
public void send(String to, String subject,
String content) throws AddressException, MessagingException {
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp host");
props.put("mail.smtp.port", "" + "smtp port");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("from address"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);
Transport.send(msg);
}
}
Flex Code :: (file name :: FlexMail)
Look into remote object in the following code. I have coded as
destination="mail"
source="testblaze.FlexMail"
destination name must be the same in both configuration file and flex code. source is the java file.
ro.getOperation("sendMail").send(toadd.text,sub.text,content.text);
the above line call the sendMail method written in java class and it sends the reqd parameters.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.utils.StringUtil;
private function sendMail():void{
ro.getOperation("sendMail").send(toadd.text,sub.text,content.text);
}
private function resultHandler(event:ResultEvent):void
{
//returned by the Java class method
Alert.show(ObjectUtil.toString(event.result));
}
private function faultHandler(event:FaultEvent):void
{
Alert.show(ObjectUtil.toString(event.fault));
}
]]>
</mx:Script>
<mx:RemoteObject id="ro"
destination="mail"
source="testblaze.FlexMail"
result="resultHandler(event)"
fault="faultHandler(event)"/>
<mx:Label x="144" y="102" text="To Address :"/>
<mx:TextInput x="252" y="100" width="191" id="toadd"/>
<mx:Label x="144" y="149" text="Subject :"/>
<mx:TextInput x="252" y="147" width="191" id="sub"/>
<mx:Label x="144" y="194" text="Content :"/>
<mx:TextArea x="252" y="193" width="191" height="69" id="content"/>
<mx:Button x="252" y="286" label="Send Mail" width="104" height="27" click="sendMail();"/>
</mx:Application>
XML ::
there are four xml files.
messaging-config.xml,proxy-config.xml,remoting-config.xml,services-config.xml.
We shall bother about only last two xml files .
remoting-config.xml::
Here we have mentioned destination id which must be the same as that of flex remote object destination. Again source is java source.
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="mail">
<properties>
<source>testblaze.FlexMail</source>
</properties>
</destination>
</service>
services-config.xml ::
in this xml you should again mention the destination. there is one more imprtant config.
<endpoint uri="http://localhost:8400/FlexMail/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
in the above tag,FlexMail is our web application.
<services-config>
<services>
<service id="remoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<destination id="mail">
<properties>
<source>testblaze.FlexMail</source>
</properties>
<channels>
<channel ref="my-amf"/>
</channels>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost:8400/FlexMail/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
No comments:
Post a Comment