uris77
7/11/2013 - 4:49 AM

How to integrating Vert-x in Grails using a PlatformManager. (Test)

How to integrating Vert-x in Grails using a PlatformManager. (Test)

//grails-app/conf/spring/resources.groovy
beans = {
    vertxPlatformManager(org.vertx.java.platform.PlatformLocator)
}
<!--grails-app/views/index.gsp-->
<!DOCTYPE html>
<html>
<head>
    <title>SockJS Test</title>
    <script src="http://cdn.sockjs.org/sockjs-0.2.1.min.js"></script>
</head>
<body>

<script>
    var sock = new SockJS('http://localhost:8585/events');
    sock.onopen = function() {
        console.log('open');
    };
    sock.onmessage = function(e) {
        console.log('message', e.data);
        alert('received message echoed from server: ' + e.data);
    };
    sock.onclose = function() {
        console.log('close');
    };

    function send(message) {

        if (sock.readyState == WebSocket.OPEN) {
            console.log("sending message")
            sock.send(message);
        } else {
            console.log("The socket is not open.");
        }
    }
</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="Hello, World!"/>
    <input type="button" value="Send SockJS data" onclick="send(this.form.message.value)"/>
</form>
</body>
</html>
//src/verticles/VerticlesTest.groovy
package verticles

def httpServer = vertx.createHttpServer()
def count = 0
vertx.createSockJSServer(httpServer).installApp(prefix: '/events') { sock ->
    sock.dataHandler { buff ->
        sock << buff
    }
}

httpServer.listen(8585)
//grails-app/conf/BuildConfig.groovy

grails.project.target.level = 1.7
grails.project.source.level = 1.7

....

/*
It's just an example of how add a vertx jars:

cd $VERTX_HOME/lib/.

mvn install:install-file -Dfile=vertx-testsuite-2.0.0-CR3.jar -DgroupId='io.vertx' -DartifactId='vertx-testsuite' -Dversion='2.0.0-CR3' -Dpackaging=jar
mvn install:install-file -Dfile=vertx-platform-2.0.0-CR3.jar -DgroupId='io.vertx' -DartifactId='vertx-platform' -Dversion='2.0.0-CR3' -Dpackaging=jar
mvn install:install-file -Dfile=vertx-core-2.0.0-CR3.jar -DgroupId='io.vertx' -DartifactId='vertx-core' -Dversion='2.0.0-CR3' -Dpackaging=jar
mvn install:install-file -Dfile=netty-all-4.0.0.CR9.jar -DgroupId='io.vertx' -DartifactId='netty-all' -Dversion='4.0.0.CR9' -Dpackaging=jar
mvn install:install-file -Dfile=jackson-databind-2.2.2.jar -DgroupId='io.vertx' -DartifactId='jackson-databind' -Dversion='2.2.2' -Dpackaging=jar
mvn install:install-file -Dfile=jackson-core-2.2.2.jar -DgroupId='io.vertx' -DartifactId='jackson-core' -Dversion='2.2.2' -Dpackaging=jar
mvn install:install-file -Dfile=jackson-annotations-2.2.2.jar -DgroupId='io.vertx' -DartifactId='jackson-annotations' -Dversion='2.2.2' -Dpackaging=jar
mvn install:install-file -Dfile=hazelcast-2.6.jar -DgroupId='io.vertx' -DartifactId='hazelcast' -Dversion='2.6' -Dpackaging=jar
*/
dependencies {        
  compile 'io.vertx:hazelcast:2.6'
  compile 'io.vertx:jackson-annotations:2.2.2'
  compile 'io.vertx:jackson-core:2.2.2'
  compile 'io.vertx:jackson-databind:2.2.2'
  compile 'io.vertx:netty-all:4.0.0.CR9'
  compile 'io.vertx:vertx-core:2.0.0-CR3'
  compile 'io.vertx:vertx-platform:2.0.0-CR3'
  compile 'io.vertx:vertx-testsuite:2.0.0-CR3'
}
//grails-app/conf/BootStrap.groovy
import org.vertx.java.platform.PlatformLocator

class BootStrap {
    def vertxPlatformManager

    def init = { servletContext ->
        vertxPlatformManager = PlatformLocator.factory.createPlatformManager()
        URL[] classpath = [new File('./src/verticles').toURI().toURL()]
        vertxPlatformManager.deployVerticle(
                'VerticlesTest.groovy', null, classpath, 1, null, null
        )
    }
    def destroy = {
    }
}