当前位置: 代码迷 >> Web前端 >> red5 调整到tomcat中(一)
  详细解决方案

red5 调整到tomcat中(一)

热度:525   发布时间:2012-06-27 14:20:09.0
red5 整合到tomcat中(一)

今天忙了一天,目的很简单,就是把red5整合到tomcat中,通过简单访问可以请求到数据。

下面简单记述一下过程:

1,下载需要的red5.red5官网http://www.red5.org/,下载1.0.rec里的两个zip包,如图。



?2,将下载的包解压后,将red5-war-1.0-RC1包里的ROOT.war放到tomcat的webapps下。将原来的ROOT文件夹改名为ROOT_t,便于区别,启动tomcat。当启动完毕后就出现一个新的ROOt文件夹。该文件夹备用。

?3,在Eclipse中新建项目。我这里命名为chapter2,然后到ROOT文件夹下 WEB-INF下面拷贝lib目录到相同位置,并拷贝文件适当修改。还要拷贝red5.jar.下面先修改web.xml.具体如下

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>chapter2</display-name>
		
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>/chapter2</param-value>
	</context-param>
	
	<context-param>
		<param-name>globalScope</param-name>
		<param-value>default</param-value>
	</context-param>

	<context-param>
		<param-name>parentContextKey</param-name>
		<param-value>default.context</param-value>
	</context-param>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/classes/*-web.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.red5.server.war.WarLoaderServlet</listener-class>
	</listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

	<servlet>
		<servlet-name>gateway</servlet-name>
		<servlet-class>org.red5.server.net.servlet.AMFGatewayServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<servlet-name>rtmpt</servlet-name>
		<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>gateway</servlet-name>
		<url-pattern>/gateway</url-pattern>
	</servlet-mapping>

    <servlet-mapping>
        <servlet-name>rtmpt</servlet-name>
        <url-pattern>/fcs/*</url-pattern>
    </servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/open/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/idle/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/send/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/close/*</url-pattern>
	</servlet-mapping>
	
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>Forbidden</web-resource-name>
			<url-pattern>/WEB-INF/*</url-pattern>
		</web-resource-collection>
		<auth-constraint />
	</security-constraint>

	<security-constraint>
		<web-resource-collection>
			<web-resource-name>Forbidden</web-resource-name>
			<url-pattern>/persistence/*</url-pattern>
		</web-resource-collection>
		<auth-constraint />
	</security-constraint>

	<security-constraint>
		<web-resource-collection>
			<web-resource-name>Forbidden</web-resource-name>
			<url-pattern>/streams/*</url-pattern>
		</web-resource-collection>
		<auth-constraint />
	</security-constraint>
	
</web-app>

?4,进入Root/WEB-INF/classes文件夹里,拷贝所有配置文件,如下.



?修改root-web.xml。具体内容如下,


?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	 <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="location" value="/WEB-INF/red5-web.properties" />
	</bean>
	<!-- ROOT web context -->
	<bean id="web.context.chapter2" class="org.red5.server.Context">
		<property name="scopeResolver" ref="red5.scopeResolver" />
		<property name="clientRegistry" ref="global.clientRegistry" />
		<property name="serviceInvoker" ref="global.serviceInvoker" />
		<property name="mappingStrategy" ref="global.mappingStrategy" />
	</bean>
	
	<bean id="web.scope.chapter2" class="org.red5.server.WebScope" init-method="register">
		<property name="server" ref="red5.server" />
		<property name="parent" ref="global.scope" />
		<property name="context" ref="web.context.chapter2" />
		<property name="handler" ref="web.handler.chapter2" />
		<property name="contextPath" value="${webapp.contextPath}" />
		<property name="virtualHosts" value="${webapp.virtualHosts}" />
	</bean>
	<bean id="web.handler.chapter2"     
             class="first.Application" singleton="true" />
             
    <bean id="flv.service" 
    	class="chapter7.StreamService" singleton="true"> </bean>

</beans>

?注意此处的chapter2,是我们的项目名称。red5-web.properties放置在web-info下,内容是

webapp.contextPath=/chapter2
webapp.virtualHosts=localhost,localhost:8088,127.0.0.1:8088

?5,修改red5-core.xml?。

具体如下:

<!-- RTMP Mina Transport -->
	<bean id="rtmpTransport" class="org.red5.server.net.rtmp.RTMPMinaTransport" init-method="start" destroy-method="stop">
		<property name="ioHandler" ref="rtmpMinaIoHandler" />
        <property name="connectors">
            <list>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="127.0.0.1" />  
                    <constructor-arg index="1" type="int" value="1935" />  
                </bean>
                <!-- You can now add additional ports and ip addresses
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  
                    <constructor-arg index="1" type="int" value="1936" />  
                </bean>
                 -->
            </list>
        </property>		
		<property name="receiveBufferSize" value="65536" />
		<property name="sendBufferSize" value="271360" />
        <property name="connectionThreads" value="4" />
        <property name="ioThreads" value="16" />
		<!-- This is the interval at which the sessions are polled for stats. If mina monitoring is not
				enabled, polling will not occur. -->
		<property name="jmxPollInterval" value="1000" />
		<property name="tcpNoDelay" value="true" />
	</bean>

?注意: tomcat里面添加太多的red5服务,会造成tomcat内存泄露,这么时候就要修改tomcat的启动内存bin/catalina.bat

?

Win:?

???????set JAVA_OPTS=%JAVA_OPTS% -Xms1024m -Xmx1024m

linux:

?????? JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxNewSize=512m -XX:MaxPermSize=512m"

?

?如果要修改webapps下的项目启动的优先级,就在? conf\Catalina\localhost? 加上一个 项目名.xml 的文件

文件内容:

?

<?xml version="1.0" encoding="UTF-8"?>  
<!--   
  Licensed to the Apache Software Foundation (ASF) under one or more   
  contributor license agreements.  See the NOTICE file distributed with   
  this work for additional information regarding copyright ownership.   
  The ASF licenses this file to You under the Apache License, Version 2.0   
  (the "License"); you may not use this file except in compliance with   
  the License.  You may obtain a copy of the License at   
  
      http://www.apache.org/licenses/LICENSE-2.0   
  
  Unless required by applicable law or agreed to in writing, software   
  distributed under the License is distributed on an "AS IS" BASIS,   
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
  See the License for the specific language governing permissions and   
  limitations under the License.   
-->  
<Context antiResourceLocking="false" privileged="true" />  

6?然后我们做相应的响应代码。

package first;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;

 

public class Application extends ApplicationAdapter{
	
	public boolean appConnect(IConnection conn,Object[] args)
	{
		System.out.println("链接");
		return true;
	}
	
	public String change(String str){
		System.out.println("客户端调用服务器端方法");
		return str.toUpperCase();
	}

}

?

package chapter7;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.springframework.core.io.Resource;

public class StreamService {
	/**
	 * Getter for property 'listOfAvailableFLVs'.
	 * 
	 * @return Value for property 'listOfAvailableFLVs'.
	 */
	public Map getListOfAvailableFLVs() {
		IScope scope = Red5.getConnectionLocal().getScope();
		Map<String, Map> filesMap = new HashMap<String, Map>();
		Map<String, Object> fileInfo;
		try {
			Resource[] flvs = scope.getResources("streams/*.flv");
			if (flvs != null) {
				for (Resource flv : flvs) {
					File file = flv.getFile();
					Date lastModifiedDate = new Date(file.lastModified());
					String lastModified = formatDate(lastModifiedDate);
					String flvName = flv.getFile().getName();
					String flvBytes = Long.toString(file.length());
					fileInfo = new HashMap<String, Object>();
					fileInfo.put("name", flvName);
					fileInfo.put("lastModified", lastModified);
					fileInfo.put("size", flvBytes);
					filesMap.put(flvName, fileInfo);
				}
			}
			Resource[] mp3s = scope.getResources("streams/*.mp3");
			if (mp3s != null) {
				for (Resource mp3 : mp3s) {
					File file = mp3.getFile();
					Date lastModifiedDate = new Date(file.lastModified());
					String lastModified = formatDate(lastModifiedDate);
					String flvName = mp3.getFile().getName();
					String flvBytes = Long.toString(file.length());
					fileInfo = new HashMap<String, Object>();
					fileInfo.put("name", flvName);
					fileInfo.put("lastModified", lastModified);
					fileInfo.put("size", flvBytes);
					filesMap.put(flvName, fileInfo);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filesMap;
	}

	private String formatDate(Date date) {
		SimpleDateFormat formatter;
		String pattern = "dd/MM/yy H:mm:ss";
		Locale locale = new Locale("en", "US");
		formatter = new SimpleDateFormat(pattern, locale);
		return formatter.format(date);
	}
}

red5 整合到tomcat中(二)

  相关解决方案