Home > Java > Red5 and JPA with Maven2

Red5 and JPA with Maven2

This article tells you how to build an application for Red5 server with Maven2. We are going to use Spring, JPA, Hibernate and PostgreSQL database. We aren’t going to use any IDE, instead we are going to use only Bash commands. I will run it on the FreeBSD server. The task of the application is not important. It’s very simple. It’s going to be a Hello World program. The application is going to check if a user has a permission to publish a video on Red5 server. The permission is granted based on sender’s IP address. Accepted IPs are stored in a PostgreSQL database. And, the best thing is that I have already written and published the app, so I’m going to write about a ready product. Let’s get started. My server has already installed:
  • Java 1.6 (Diablo JDK)
  • Maven 2.0.9
  • PostgreSQL 8.3
You have to get the application sources from SVN:  
svn checkout http://jakubiak-red5.googlecode.com/svn/trunk/ jakubiak-red5-read-only
    This way, you get sources of jakubiak-red5 project, which include our example application. jakubiak-red5 project is linked to Red5 sources using svn:externals. This mean, Red5 sources are downloaded together with my project. Now it’s time for the first compilation. Thanks to a Marcello Teodorii’s contribution the application is built without any problems.  
cd jakubiak-red5-read-only/jakubiak-red5-pom/
mvn clean install
    Now, maven starts getting dependencies for the application from the Internet. It can take some time. After downloading, we have to wait for compilation of about 700 Java classes. At the end, we will see a success message. (As you can see, I have been waiting 16 minutes, however it’s built much faster for second time.) Now, you have to create PostgreSQL a database and a user. As a password type demojpa.  
createuser -h localhost -U pgsql -S -D -R -P demojpa
createdb -h localhost -U pgsql -O demojpa demojpa
    You have to create the database now, because it’s necessary for compiling and testing the application.  
cd ../jakubiak-red5-demo-jpa/
mvn clean install
    You have to wait few minutes. You have just built the application. The result of maven tasks is a war file. You have to deploy the war file on Apache Tomcat. I prefer 6.0.18 version.  
cd ../..
wget "http://ftp.tpnet.pl/vol/d1/apache/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz"
tar xvfz apache-tomcat-6.0.18.tar.gz
cp jakubiak-red5-read-only/jakubiak-red5-demo-jpa/target/tomcat/webapps/jakubiak-red5-demo-jpa-war.war apache-tomcat-6.0.18/webapps/
    You make Tom cat do it’s job. Mrrrau :)  
cd apache-tomcat-6.0.18/
./bin/startup.sh
tail -f logs/catalina.out
    In Tomcat log we are looking for a success. It’s time for testing. You have to start a web browser:  
http://ADRES-IP-SERWERA:8080/jakubiak-red5-demo-jpa-war/publisher.swf
    It loads a standard application distributed with Red5 – publisher. In “Location” field type:  
rtmp://ADRES-IP-SERWERA/demojpa
    RTMP is a protocol used by Flash applications. “demojpa” is an accessible name of our application. Press “Connect” button. On the “Settings” tab you have to select a web camera and a microphone. You have to test publishing. It shouldn’t work, because your IP address doesn’t have permission for the application. We can find a warning message in Apache Tomcat logs. You have to put your IP address in the database.  
psql -h localhost -U demojpa
INSERT INTO hostallow VALUES ( '192.168.0.114' );
    Let’s go back to the web browser. Now, you can publish the video and admire the results. Now you have a working application. It’s time for you to learn.  
find . -type f | egrep -v .svn
./src/test/java/eu/jakubiak/red5/demo/jpa/StreamPublishSecurityTest.java
./src/main/java/eu/jakubiak/red5/demo/jpa/entities/HostAllow.java
./src/main/java/eu/jakubiak/red5/demo/jpa/DemojpaApplicationAdapter.java
./src/main/java/eu/jakubiak/red5/demo/jpa/StreamPublishSecurity.java
./src/main/resources/META-INF/persistence.xml
./src/main/resources/red5-services.xml
./src/main/resources/logback.xml
./src/main/resources/red5-core.xml
./src/main/resources/red5-common.xml
./src/main/resources/beanRefContext.xml
./src/main/resources/defaultContext.xml
./src/main/resources/red5-web.xml
./src/main/resources/jdbc.properties
./src/main/webapp/META-INF/MANIFEST.MF
./src/main/webapp/WEB-INF/demojpa-servlet.xml
./src/main/webapp/WEB-INF/web.xml
./src/main/webapp/publisher.swf
./pom.xml
    From the beginning, StreamPublishSecurityTest – is a Java class for testing sensitive code in your application. HostAllow is a JPA entity class defining a table in the database. DemojpaApplicationAdapter defines a starting point for Red5 application, where I have placed a code enabling the security rules. StreamPublishSecurity is a real worker class – it checks the security rules against the database. Next: persistence.xml is a file for JPA, you put entity classes there. red5-services.xml is a very important file, where we you configure Spring beans used by your application. There are also definitions of the database connection. Also, you can put Red5 remote services there. logback.xml is a log system configuration. red5-core.xml, red5-common.xml, beanRefContext.xml and defaultContext.xml are configuration files for Spring beans used by Red5. If you want to improve Red5 this is the place to hack. red5-web.xml is a file where you can change RTMP name of your application – “demojpa”. jdbc.properties is a file with database properties. demojpa-servlet.xml is a file for defining MVC controllers of your web application. web.xml is the most important file for war archive, it defines web application components. We have dealt with publisher.swf earlier. pom.xml is a magic Maven file, which describes how maven has to build the application. Cool, isn’t it?
Categories: Java Tags: , , , ,
  1. Yevgeny Chertov
    February 11th, 2011 at 22:48 | #1

    great article Antoni,

    This subject is not published about and you might be the first on the net. Great job. I am having a minor issue that I would appreciate help in resolving. at the step when you do:

    cd ../jakubiak-red5-demo-jpa/
    mvn clean install

    I get a build error:

    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Failed to resolve artifact.

    Missing:
    ———-
    1) eu.jakubiak:jakubiak-red5-flash-publisher:swf:0.9.2-SNAPSHOT

    and im just somewhat confused (maybe cause I’m new to maven)

    Thank you very much, and even if you dont help me great job with the article.

    Yev

  2. February 13th, 2011 at 20:31 | #2

    I have just submitted a small patch. Now it shall compile.

  3. choesang
    February 17th, 2011 at 18:02 | #3

    Hi!
    I am stuck at the same point as Yevgeny.

    I downloaded the code, did mvn clean install on “jakubiak-red5-pom”.
    Installed postgresql and created user “demojpa”

    But throws an error at

    c:\workspace\jakubiak\jakubiak-red5-demo-jpa>mvn clean install
    [INFO] Scanning for projects…
    [INFO]
    [INFO] ————————————————————————
    [INFO] Building jakubiak-red5-demo-jpa 1.0-SNAPSHOT
    [INFO] ————————————————————————
    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 1.329s
    [INFO] Finished at: Thu Feb 17 17:56:02 CET 2011
    [INFO] Final Memory: 5M/121M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal on project jakubiak-red5-demo-jpa: Could not resolve dependencies for project eu.jakubiak:jakubiak-red5-demo-jpa:war:1.0-SNAPSHOT: Failed to collect dependencies for [eu
    .jakubiak:jakubiak-red5-annotations:jar:0.9.2-SNAPSHOT (compile), eu.jakubiak:jakubiak-red5-patches:jar:0.9.2-SNAPSHOT (compile), eu.jakubiak:jakubiak-red5-core:jar:0.9.2-SNAPSHOT (provided), eu.jakub
    iak:jakubiak-red5-flash-publisher:swf:0.9.2-SNAPSHOT (compile), javax.persistence:persistence-api:jar:1.0 (provided), org.springframework:spring-context-support:jar:3.0.2.RELEASE (compile), org.spring
    framework:spring-tx:jar:3.0.2.RELEASE (compile), org.springframework:spring-orm:jar:3.0.2.RELEASE (compile), org.springframework:spring-jdbc:jar:3.0.2.RELEASE (compile), org.springframework:spring-tes
    t:jar:3.0.2.RELEASE (test), junit:junit:jar:4.7 (test), org.springframework:spring-webmvc:jar:3.0.2.RELEASE (compile), javax.servlet:servlet-api:jar:2.5 (provided), javax.servlet:jstl:jar:1.2 (compile
    ), javax.servlet.jsp:jsp-api:jar:2.1 (provided), commons-fileupload:commons-fileupload:jar:1.2.1 (compile), commons-io:commons-io:jar:1.4 (compile), antlr:antlr:jar:2.7.7 (compile), org.antlr:antlr:ja
    r:3.0.1 (compile), postgresql:postgresql:jar:8.3-603.jdbc4 (compile), cglib:cglib-nodep:jar:2.2 (compile), org.hibernate:hibernate-entitymanager:jar:3.4.0.GA (compile), org.hibernate:hibernate-annotat
    ions:jar:3.4.0.GA (compile), c3p0:c3p0:jar:0.9.1.2 (compile)]: Failed to read artifact descriptor for eu.jakubiak:jakubiak-red5-flash-publisher:swf:0.9.2-SNAPSHOT: Failure to find eu.jakubiak:jakubiak
    -red5-flash-pom:pom:0.9.2-SNAPSHOT in http://repository.sonatype.org/content/groups/flexgroup was cached in the local repository, resolution will not be reattempted until the update interval of flexmo
    jos has elapsed or updates are forced -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

    I even tried building individual pom files but didnt help.
    Do you have any idea where it is going wrong?

    Thanks for the great tutorial.

  4. February 17th, 2011 at 18:14 | #4

    You have to build: jakubiak-red5-flash-publisher

  5. VJ
    April 26th, 2011 at 08:43 | #5

    I have a requirement where users can stream videos from red5 on flash plugin. The video can be switched to pay mode anytime. For instance, if a unpaid user is watching this video and the mode is changed to paymode, the user should be alerted about this change. will it be possible to send requests to reauthenticate at regular intervals to server?

  6. Joris
    June 14th, 2011 at 13:08 | #6

    I checked out the trunk of http://jakubiak-red5.googlecode.com/svn but when I go to the jakubiak-red5-pom directory and run

    mvn clean install

    I get a large number of compilation errors in jakubiak-red5-core:

    \workspace\red5-project\jakubiak-red5\jakubiak-red5-core\src\main\java\org\red5\server\net\remoting\RemotingClient.java:[29,22] package org.apache.http does not exist

    (…)

    \workspace\red5-project\jakubiak-red5\jakubiak-red5-core\src\main\java\org\red5\io\m4a\impl\M4AReader.java:[334,29] cannot find symbol
    symbol : variable ToStringBuilder
    location: class org.red5.io.m4a.impl.M4AReader

    This is my Maven version: mvn -version
    Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)
    Java version: 1.6.0_14
    Java home: C:\Program Files\Java\jdk1.6.0_14\jre
    Default locale: nl_NL, platform encoding: Cp1252
    OS name: “windows xp” version: “5.1″ arch: “x86″ Family: “windows”

  7. Jeremy
    September 12th, 2011 at 13:05 | #7

    Same for me :

    [INFO] ————————————————————————
    [INFO] Building jakubiak-red5-core
    [INFO] task-segment: [clean, install]
    [INFO] ————————————————————————
    [INFO] [clean:clean {execution: default-clean}]
    [INFO] Deleting directory /Users/jeremyfinckbohner/TAFF/Projets/jakubiak-red5/svn (trunk)/jakubiak-red5-core/target
    [INFO] [resources:resources {execution: default-resources}]
    [INFO] Using ‘UTF-8′ encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /Users/jeremyfinckbohner/TAFF/Projets/jakubiak-red5/svn (trunk)/jakubiak-red5-core/src/main/resources
    [INFO] [compiler:compile {execution: default-compile}]
    [INFO] Compiling 524 source files to /Users/jeremyfinckbohner/TAFF/Projets/jakubiak-red5/svn (trunk)/jakubiak-red5-core/target/classes
    [INFO] ————————————————————-
    [ERROR] COMPILATION ERROR :
    [INFO] ————————————————————-
    [ERROR] /Users/jeremyfinckbohner/TAFF/Projets/jakubiak-red5/svn (trunk)/jakubiak-red5-core/src/main/java/org/red5/server/net/remoting/RemotingClient.java:[29,22] package org.apache.http does not exist

    [...]

    @Joris

  1. No trackbacks yet.