Embedded Server - aemadrid/orientdb GitHub Wiki

Embed the server in Java applications

Introduction

Embedding an OrientDB Server inside a Java application has several advantages and interesting features:

  • Java application that runs embedded with the server can bypass the remote connection and use the database directly with local mode. local and remote connections against the same database can work in concurrency: OrientDB will synchronize the access.
  • You can use the Console to control it
  • You can use the OrientDB Studio

To embed an OrientDB Server inside a Java application you have to create the OServer object and use a valid configuration for it.

Requirements

In order to embed the server you need to include the following JARs in the classpath:

  • orientdb-enterprise-**.jar
  • orientdb-server-**.jar

Include the commands you need

Even if most of the HTTP commands are auto registered assure to have all the commands you need. For example the static content must be registered. This is fundamental if you want to use OrientDB as Web Server providing static content like the Studio app:

    <listener protocol="http" port-range="2480-2490" ip-address="0.0.0.0">
      <commands>
        <command implementation="com.orientechnologies.orient.server.network.protocol.http.command.get.OServerCommandGetStaticContent" pattern="GET|www GET|studio/ GET| GET|*.htm GET|*.html GET|*.xml GET|*.jpeg GET|*.jpg GET|*.png GET|*.gif GET|*.js GET|*.css GET|*.swf GET|*.ico GET|*.txt GET|*.otf GET|*.pjs GET|*.svg">
          <parameters>
            <entry value="Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\nPragma: no-cache" name="http.cache:*.htm *.html"/>
            <entry value="Cache-Control: max-age=120" name="http.cache:default"/>
          </parameters>
        </command>
      </commands>
    </listener>

Use an embedded configuration

    import com.orientechnologies.orient.server.OServerMain;
    
    public class OrientDBEmbeddable {
    
     public static void main(String[] args) throws Exception {
      OServerMain.create().startup(
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
       + "<orient-server>"
       + "<network>"
       + "<protocols>"
       + "<protocol name=\"binary\" implementation=\"com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary\"/>"
       + "<protocol name=\"http\" implementation=\"com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpDb\"/>"
       + "</protocols>"
       + "<listeners>"
       + "<listener ip-address=\"0.0.0.0\" port-range=\"2424-2430\" protocol=\"binary\"/>"
       + "<listener ip-address=\"0.0.0.0\" port-range=\"2480-2490\" protocol=\"http\"/>"
       + "</listeners>"
       + "</network>"
       + "<users>"
       + "<user name=\"root\" password=\"ThisIsA_TEST\" resources=\"*\"/>"
       + "</users>"
       + "<properties>"
       + "<entry name=\"orientdb.www.path\" value=\"C:/work/dev/orientechnologies/orientdb/releases/1.0rc1-SNAPSHOT/www/\"/>"
       + "<entry name=\"orientdb.config.file\" value=\"C:/work/dev/orientechnologies/orientdb/releases/1.0rc1-SNAPSHOT/config/orientdb-server-config.xml\"/>"
       + "<entry name=\"server.cache.staticResources\" value=\"false\"/>"
       + "<entry name=\"log.console.level\" value=\"info\"/>" + "<entry name=\"log.file.level\" value=\"fine\"/>"
       + "</properties>" + "</orient-server>");
    	}
    }

Use custom file for configuration

Use a regular File:

    public class OrientDBEmbeddable {
    
     public static void main(String[] args) throws Exception {
      OServer server = OServerMain.create();
      server.startup(new File("/usr/local/temp/db.config"));
      server.activate();
     }
    }

Or an InputStream from the classloader:

    public class OrientDBEmbeddable {
    
     public static void main(String[] args) throws Exception {
      OServer server = OServerMain.create();
      server.startup(getClass().getResourceAsStream("db.config"));
      server.activate();
     }
    }

Shutdown

OrientDB Server creates some threads internally as non-daemon threads, so they runs even if the main application exists. Use the OServer.shutdown() method to shutdown the server in soft way:

    import com.orientechnologies.orient.server.OServerMain;
    
    public class OrientDBEmbeddable {
    
     public static void main(String[] args) throws Exception {
      OServer server = OServerMain.create();
      server.startup(new File("/usr/local/temp/db.config"));
      server.activate();
      ...
      server.shutdown();
     }
    }
⚠️ **GitHub.com Fallback** ⚠️