GlassFish的嵌入式开发

来源:互联网 发布:打开淘宝商品链接 编辑:程序博客网 时间:2024/05/17 01:48

1.下载glassfish-embedded-all-3.0.1.jar

2.将其加入到你的classpath下

演示代码如下

首先写个say hello的EJB

package com;

import javax.ejb.Stateless;

/**
*
* @author Daniel Yang
*/
@Stateless
public class MyEJB {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

再写个Main函数测试

    package ejbtest;

import com.MyEJB;
import java.io.File;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.glassfish.api.embedded.ContainerBuilder;
import org.glassfish.api.embedded.EmbeddedFileSystem;
import org.glassfish.api.embedded.ScatteredArchive;
import org.glassfish.api.embedded.Server;

/**
*
* @author Daniel Yang
*/
public class Main {

  static final String instanceRoot = "D:/myapp/EmbeddedGlassFish/domains/domain1";
    static final String configFile = "D:/myapp/EmbeddedGlassFish/domains/domain1/config/domain.xml";
    static final String archivePath = "D:/projects/EJBTest/build/classes"; //你的EJB bean所编译后的位置, Netbean ant build后

//在build/classes下,而用Netbeans maven build后在target目录,所以不一样,找到它,指着它就行了

    public Main() {
    }

    public static void main1(String args[]) throws Exception {
        EmbeddedFileSystem.Builder efsBuilder = new EmbeddedFileSystem.Builder();
        efsBuilder.instanceRoot(new File(instanceRoot));
        efsBuilder.configurationFile(new File(configFile));
        Server.Builder builder = new Server.Builder("test");
        builder.embeddedFileSystem(efsBuilder.build());
        Server server = builder.build();
        try {
            server.addContainer(ContainerBuilder.Type.ejb);
            File f = new File(archivePath);
            ScatteredArchive archive = new ScatteredArchive.Builder("HelloWorld",
                    f).buildJar();
            server.start();
            server.getDeployer().deploy(archive, null);
            Context context = new InitialContext();
            MyEJB test = (MyEJB) context.lookup("java:global/HelloWorld/MyEJB");
            if (test != null) {
                String greeting = test.sayHello("Duke");
                System.out.println("==================================");
                System.out.println(greeting);
                System.out.println("==================================");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            server.getDeployer().undeployAll();
            server.stop();
            System.exit(0);
        }
    }
}

结果如下

run:
2010-11-16 0:22:17 com.sun.grizzly.config.GrizzlyEmbeddedHttps configureProtocol
信息: Perform lazy SSL initialization for the listener 'http-listener-2'
2010-11-16 0:22:17 com.sun.grizzly.Controller logVersion
信息: Starting Grizzly Framework 1.9.18-o - Tue Nov 16 00:22:17 CST 2010
2010-11-16 0:22:17 com.sun.grizzly.Controller logVersion
信息: Starting Grizzly Framework 1.9.18-o - Tue Nov 16 00:22:17 CST 2010
2010-11-16 0:22:17 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady
信息: Grizzly Framework 1.9.18-o started in: 47ms listening on port 7676
2010-11-16 0:22:17 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady
信息: Grizzly Framework 1.9.18-o started in: 140ms listening on port 8080
2010-11-16 0:22:17 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady
信息: Grizzly Framework 1.9.18-o started in: 93ms listening on port 8181
2010-11-16 0:22:17 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady
信息: Grizzly Framework 1.9.18-o started in: 93ms listening on port 4848
2010-11-16 0:22:17 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady
信息: Grizzly Framework 1.9.18-o started in: 62ms listening on port 3700
2010-11-16 0:22:18 com.sun.enterprise.v3.server.AppServerStartup run
信息: GlassFish Server Open Source Edition 3.0.1 (java_re-private) startup time : Embedded(406ms) startup services(610ms) total(1016ms)
2010-11-16 0:22:18 com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates
信息: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate
2010-11-16 0:22:18 org.glassfish.admin.mbeanserver.RMIConnectorStarter startRegistry
信息: Binding RMI port to *:8686
2010-11-16 0:22:18 AppServerStartup run
信息: [Thread[GlassFish Kernel Main Thread,5,main]] started
2010-11-16 0:22:18 org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread startConnector
信息: JMXStartupService: Started JMXConnector, JMXService URL = service:jmx:rmi://DANIEL:8686/jndi/rmi://DANIEL:8686/jmxrmi
2010-11-16 0:22:18 com.sun.enterprise.security.SecurityLifecycle <init>
信息: security.secmgroff
2010-11-16 0:22:19 com.sun.enterprise.security.SecurityLifecycle onInitialization
信息: Security startup service called
2010-11-16 0:22:19 com.sun.enterprise.security.PolicyLoader loadPolicy
信息: policy.loading
2010-11-16 0:22:19 com.sun.enterprise.security.auth.realm.Realm doInstantiate
信息: Realm admin-realm of classtype com.sun.enterprise.security.auth.realm.file.FileRealm successfully created.
2010-11-16 0:22:19 com.sun.enterprise.security.auth.realm.Realm doInstantiate
信息: Realm file of classtype com.sun.enterprise.security.auth.realm.file.FileRealm successfully created.
2010-11-16 0:22:19 com.sun.enterprise.security.auth.realm.Realm doInstantiate
信息: Realm certificate of classtype com.sun.enterprise.security.auth.realm.certificate.CertificateRealm successfully created.
2010-11-16 0:22:19 com.sun.enterprise.security.SecurityLifecycle onInitialization
信息: Security service(s) started successfully....
2010-11-16 0:22:19 com.sun.ejb.containers.BaseContainer initializeHome
信息: Portable JNDI names for EJB MyEJB : [java:global/HelloWorld/MyEJB, java:global/HelloWorld/MyEJB!com.MyEJB]
==================================
Hello, Duke
==================================

 

 

 

原创粉丝点击