CMT -- use Java and flex for C/S program 1

Source: Internet
Author: User
Tags cdata
  • Project Background of CMT:

  • Java has been running for three years. I feel that the application scenarios of Java are mainly limited to the B/S program, because using Java for C/S programs is not only troublesome, but also slow, it is incompatible with the operating system interface. Eclipse's SWT runs much faster than swing, but its poor thread encapsulation and tedious Control calling make development of the swt interface very troublesome.

    It has been nearly a year since I came into contact with flex for work reasons. Although his syntax is not so perfect as compared with Java, each language has its strengths, and flex is no exception, using it for interface development not only provides fast development speed, beautiful interface, and fast operation speed, but also facilitates integration with other systems, after all, the SWF compiled by FLEX can be played Using Flash ActiveX and can be integrated with any platform that supports ActiveX containers.

     

    For the above reason, the idea of integrating flex with the Java platform to complement each other makes it easy to call each other, just like calling a local method. Currently based on

    The Web Service flex library allows flex to call web service and call other platforms. However, this call is only one-way and the service provider cannot call flex, as a C/S program, distribution and configuration are also cumbersome.

    Implementation principle of CMT:

    Flex calls Java:

    Serialize the local flex object to the corresponding XML string, and then send the string to Java through a socket. After receiving the string, Java parses the string, converts the string to the corresponding Java object through the reflection mechanism, and finally calls the corresponding method.

    Java calls flex:

    Similar to calling Java using Flex

    Basic Development Process:

    Let's take a look at the basic development steps :)

    Java:

    1. Open eclipse to create a Java project.

    2: import the required libraries cmt. jar and dom4j. jar.

     

    Flex

    :

    Create a new Web application and import the CMT. SWC library to this release

    Figure 1:

     

    Figure 2:

     

     

     

     Flex calls Java:

    1: Create the test. ivktest class in the Java project as a service class. The Code is as follows:

     

    Package test;

    Import java. util. date;
    Import java. util. List;
    Import java. util. Map;

    Import org. communicator. server. channelfactory;

    Public class ivktest {
    /**
    * FLEX will call this method, which will simply return a string
    * @ Param Input
    * @ Return
    */
    Public String Hello (string input ){
    System. Out. println (input );
    Return "Java return ";
    }
    /**
    * Flex calls this method and passes in all data types that flex can interact with the platform.
    *
    * @ Param BL
    * @ Param it
    * @ Param uit
    * @ Param num
    * @ Param dt
    * @ Param Str
    * @ Param lst
    * @ Param Map
    * @ Return this method returns a simple bean object. The attributes of the bean object include all simple data types that Java can interact with the platform.
    */
    Public typestest testflextojava (Boolean BL, int it, long uit, double num,
    Date DT, string STR, list lst, map ){
    Return new typestest (); // return a custom object
    }
    Public static void main (string [] ARGs ){
    // You only need the following code to open the data channel factory of the Java Server and wait for the connection from the flex client.
    Channelfactory. INIT ();

    // Endless loop to prevent program termination
    While (true ){
    Try {
    Thread. Sleep (1000 );

    } Catch (interruptedexception e ){
    E. printstacktrace ();
    }
    }
    }
    }
     

     

    Later, flex will call the hello and testflextojava methods of this class.

    2: Create a test package in the flex project, which corresponds to the package of the Java ivktest class.

    3: Create an ivktest class in this package (this class is the proxy class of the Java ivktest class. When calling this class method, it will call the corresponding Java method to complete specific functions, this type is widely used in real projects)

    The code for this class is as follows:

     

    Package test
    {
    Import MX. Collections. arraycollection;

    Import org. communicator. server. baseivkserver;

    Public class ivktest extends baseivkserver // All proxy classes are inherited from the baseivkserver class, which encapsulates the specific call details
    {
    Public Static Var URL: String = "127.0.0.1: 1236"; // Java server address, followed by an IP address and a port
    Public Static Var instance: ivktest = new ivktest (); // In order to simulate the single-column mode, the static fields of this class are created here.
    Public Function ivktest ()
    {
    }
    /**
    This method is a simple proxy method. When this method is called, it will automatically call the hello method of Java's test. ivktest class, and press the program
    Each element is input as a parameter of the hello method. After the return result of Java, the resfun as the callback function is automatically called to complete the corresponding function.
    The URL is the network address of the Java Server.
    "Hello" is the name of the called method.
    ARGs is the parameter passed in when the Java method is called. The order is the same as that in The args array.
    * Resfun is the method automatically called by the system after the call is returned.
    */
    Public Function Hello (STR: String, resfun: function): String {
    VaR ARGs: array = [STR];
    This. sendrequest (URL, "hello", argS, resfun );
    Return NULL;
    }
    /**
    * And hello method types
    */
    Public Function testflextojava (BL: Boolean, it: int, uit: uint, num: Number, DT: date, STR: String
    , AR: arraycollection, MAP: Object): void {

    VaR ARGs: array = [BL, IT, uit, num, DT, STR, AR, MAP];
    This. sendrequest (URL, "testflextojava", argS, function RET (RET: Object): void {
    Trace (RET );
    });
    }
    }
    }

     

    4: the code for creating a cmttest. mxml application file in the flex project is as follows:

     

    <? XML version = "1.0" encoding = "UTF-8"?>
    <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
    <Mx: Initialize>
    <! [CDATA [
    // Channel ID. the Java Server can identify different channels by using this identifier.
    VaR channelport: Int = 22;
    // Create a data channel
    VaR channel: datachannel = datachannelfactory. create_channel ("127.0.0.1", 1236, channelport );
    // The this. onconnect method is called after the data channel is connected to Java.
    Channel. onopenfun = This. onconnect;
    // Connect to Java and open the data channel
    Channel. open ();
    ]>
    </MX: Initialize>
    <Mx: SCRIPT>
    <! [CDATA [
    Import org. communicator. datachannel;
    Import MX. Collections. arraycollection;
    Import test. ivkflex;
    Import MX. Controls. Alert;
    Import test. ivktest;
    Import org. communicator. channelurl;
    Import org. communicator. datachannelfactory;
    // Here, the purpose of declaring an ivkflex object is to enable the flex reflection mechanism to successfully create an object of this type.
    VaR ivkflex: ivkflex;
    // This method is called when the button is clicked.
    Function onclick (): void {
    Ivktest. instance. Hello ("Hello, I am Flex", function RET (Res: string): void {
    Alert. Show (RES );
    });
    }
    /**
    * Request Java's test. ivktest. testflextojava method to test all data types
    */
    Function onconnect (): void {
    VaR AR: arraycollection = new arraycollection (['1', '2', '3']);
    VaR AR2: arraycollection = new arraycollection (['1adf', '2', '3df ']);
    Ivktest. instance. testflextojava (false, 1, 2, 34.45, new date (), "str", AR, {one: Ar, SEC: AR2 });
    }
    ]>
    </MX: SCRIPT>
    <Mx: button label = "ivktest" Click = "onclick ()"/>
    </MX: Application>

     

 

Connect to the Java data channel. When the button is clicked, The onclick () method calls the hello method of the ivktest class. After the Java return, the simple alert generates the Java return.

Java calls flex:

This is the most brilliant use of the same data channel, which can implement interaction between Java and flex, making Java + flex a C/S program possible.

1: create a service type ivkflex under the test package of flex for Java to call. The specific code is as follows:

Package test
{
Import MX. Controls. Alert;

Public class ivkflex
{
Public Function ivkflex ()
{
}
// Java will call this method
Public Function Test (STR: string): String {
Alert. Show ("Java called me and passed in" + Str );
Return 'flex return ';
}
}
}

 

 

2: Create a proxy type in Java (the package name is the same as the class name). The Code is as follows:


Package test;

Import org. communicator. server. baseivkserver;
Import org. communicator. server. channelfactory;

Public class ivkflex extends baseivkserver {

Public ivkflex (){

}
/**
* This method calls the test method in the test. ivkflex class of the Flex client whose data channel ID is 22.
* @ Param info
* @ Return
*/
Public String test (string info ){
// Input parameters
String [] par = new string [] {info };
// The channel ID must be consistent with the channel ID of the Flex Client
Int flexclientid = 22;
// Stop the current thread until flex returns data
Object ret = This. sendmiddval (flexclientid, "test", par). getreturn ();
// Return data
Return ret. tostring ();
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// You only need the following code to open the data channel factory of the Java Server and wait for the connection from the flex client.
Channelfactory. INIT ();

// Endless loop to prevent program termination
While (true ){
Try {
Thread. Sleep (1000 );

} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}

}



Click here to download the Development Kit


 



Welcome to the cmt qq Group 90636900 for discussion.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.