Tag Archives: Java

How to install Red5 on Cent Os 5.4

Red5 is open source flash server written in java supports streaming audio/video, recording client streams, shared objects, live stream publishing etc. Here I will describe in details how you can install Red5 on your CentOS. For this you will also need to install Java and Apache Ant.

Step 1. Install Java

RED5 server depends on Java. CentOS 5.3 comes with OpenJDK 1.6 and install it using yum.

yum -y install java-1.6.0-openjdk java-1.6.0-openjdk-devel

Step 2. Install Ant

Ant will need to compile RED5 server code. Ant comes in binary form, so just download and install it in /usr/local directory.

cd /usr/src
wget http://opensource.become.com/apache/ant/binaries/apache-ant-1.7.1-bin.tar.gz
tar zxvf apache-ant-1.7.1-bin.tar.gz
mv apache-ant-1.7.1/ /usr/local/ant

Step 3. Export Variables for Ant and Java

export ANT_HOME=/usr/local/ant
export JAVA_HOME=/usr/lib/jvm/java
export PATH=$PATH:/usr/local/ant/bin
export CLASSPATH=.:$JAVA_HOME/lib/classes.zip

Also export these variables in /etc/bashrc to become available for every user login or for any terminal opens.

echo ‘export ANT_HOME=/usr/local/ant’ >> /etc/bashrc
echo ‘export JAVA_HOME=/usr/lib/jvm/java’ >> /etc/bashrc
echo ‘export PATH=$PATH:/usr/local/ant/bin’ >> /etc/bashrc
echo ‘export CLASSPATH=.:$JAVA_HOME/lib/classes.zip’ >> /etc/bashrc

Step 4. Download and Install RED5 Server

Here the latest version available for RED5 is 0.7 on site but download from google code using svn as the tarball of 0.7 on site is missing some of the files.

cd /usr/src
svn checkout http://red5.googlecode.com/svn/java/server/trunk/ red5
mv red5 /usr/local/
cd /usr/local/red5
ant prepare
ant dist

you will see a ton of lines, but you should get at last

BUILD SUCCESSFUL

that’s mean its install and now copy the conf directory from dist/ and test the red5 installation.

cp -r dist/conf .
./red5.sh

If it shows Installer service created in the last then everything is fine here, press ctrl+c and move to next step to create init script.

Step 5. Init Script

Now we will create init script for red5 to start, stop and restart easily.

vi /etc/init.d/red5

The init script code is below.

#!/bin/sh
# For RedHat and cousins:
# chkconfig: 2345 85 85
# description: Red5 flash streaming server
# processname: red5

PROG=red5
RED5_HOME=/usr/local/red5
DAEMON=$RED5_HOME/$PROG.sh
PIDFILE=/var/run/$PROG.pid

# Source function library
. /etc/rc.d/init.d/functions

[ -r /etc/sysconfig/red5 ] && . /etc/sysconfig/red5

RETVAL=0

case “$1″ in
start)
echo -n $”Starting $PROG: ”
cd $RED5_HOME
$DAEMON >/dev/null 2>/dev/null &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo $! > $PIDFILE
touch /var/lock/subsys/$PROG

fi
[ $RETVAL -eq 0 ] && success $”$PROG startup” || failure $”$PROG startup”
echo
;;
stop)
echo -n $”Shutting down $PROG: ”
killproc -p $PIDFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
;;
restart)
$0 stop
$0 start
;;
status)
status $PROG -p $PIDFILE
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
RETVAL=1
esac

exit $RETVAL

Now start the service

/etc/init.d/red5 start

check status

/etc/init.d/red5 status
red5 (pid XXXXX) is running…

again you can do stop, restart.

Step 6. Test

Now test the RED5 installation by opening following URL in browser

http://yourip:5080/

You will see Red5 test page.

Sending Email From Your Spring Application

The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.

In my last project I have used spring mail functionality to send mail to the user. This is the simplest way so far I have found to send mail from spring application.

Let us also assume that there is a requirement stating that an email message with login information needs to be sent to the user.

Basic MailSender and SimpleMailMessage usage

Sample Controller:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ForgotPasswordFormController implements Controller{
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage;

public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
ModelAndView forgotPasswordMav = new ModelAndView();
forgotPasswordMav.setViewName(”forgot”);

if(request.getMethod().equalsIgnoreCase(”post”)){

// Do the business calculations…

// Create a thread safe “copy” of the template message and customize it

String email = request.getParameter(”email”);
SimpleMailMessage msg = new SimpleMailMessage(this.simpleMailMessage);
msg.setTo(email);
msg.setText(
“Dear user,”
+ “Your requested login information are following”
+ “Username :” + username
+ “Password :” + password
+ “, thank you for being with us ”
+ “Thanking,”
+ “Customer Care”);
try{
this.mailSender.send(msg);
status = 1;
}
catch(MailException ex) {
// simply log it and go on…
System.err.println(ex.getMessage());
}
}

return forgotPasswordMav;
}

}

Find below the bean definitions for the above code:

<bean name=”/forgotPassword.do” class=”com.mycompany.businessapp.ForgotPasswordFormController”>
<property name=”mailSender” ref=”mailSender”></property>
<property name=”simpleMailMessage” ref=”simpleMailMessage”></property>
</bean>
<bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>
<property name=”host” value=”mail.yourdomain.com”/>
</bean>

<!–
this is a template message that we can pre-load with default state
–>
<bean id=”simpleMailMessage” class=”org.springframework.mail.SimpleMailMessage”>
<property name=”from” value=”youremail@yourdomain.com” />
<property name=”subject” value=”Your Login Information” />
</bean>

To use this you will need the following two jars in your /WEB-INF/lib

mail.jar
activation.jar

To learn more or other ways of sending mail from spring application you can visit the following link:
Click Here

Itegrating DWR (Direct Web Remoting) with Spring

Hi Springers, I have used DWR (Direct Web Remoting) in my last project which was pretty interesting and I want to share the simplest way of using DWR with spring application. Download the latest version of dwr.jar from here. And put it in the /WEB-INF/lib folder. I have used DWR 2.0.5.

Add the following lines in your web.xml

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR Servlet</display-name>
  <servlet-class>
    org.directwebremoting.servlet.DwrServlet
  </servlet-class>
  <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Now create a dwr.xml in the WEB-INF folder alongside web.xml and add the following line in that file.

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
    <create creator="new" javascript="JDate">
      <param name="class" value="java.util.Date"/>
    </create>
    <create creator="new" javascript="Demo">
      <param name="class" value="your.java.Bean"/>
    </create>
  </allow>
</dwr>

To check go to the following URL.

http://localhost:8080/[YOUR-WEBAPP]/dwr/

You should see a page showing you the classes that you’ve selected.

To learn detail about DWR please visit the documentation site of DWR.

How to install JDK in Ubuntu

Those who are involved in Java based application development must need to use Java Development Kit (JDK). And those are newbie in the world of Ubuntu may don’t know how to install JDK in Ubuntu. This is quite simple to install JDK in ubuntu. Just run the following command in the terminal and follow the steps.

$sudo apt-get install sun-java5-jdk

After this the rest of the process will display a dialog that will require you to accept the license agreement. When you do, the rest of the setup will happen on its own.

When you’re on the command prompt type

javac -version

or

java -version

Now you should be ready to work with Java.