Integration of Memcached and Wowza projects

Source: Internet
Author: User
Tags connection pooling memcached object object wowza log4j

Cluster is a required course of Wowza deployment, the Wowza plug-in in cluster environment relies on the cache used Memcached as a solution is a good choice. This article describes how to add Memcached support to the Wowza plug-in development project, with regard to Memcached data atomicity, Memcached clusters, and the Java in-process cache. This article demonstrates the sample code with the Spymemcached 2.8.4.
This article integrates the Memcached with the Demo sample project using the Spring integrated DBCP database connection pool to Wowza plugin. The blog code: http://download.csdn.net/detail/defonds/7098633.
1. Import the previous Blog Demo sample code
Import the Demo sample code using the Spring integrated DBCP database connection pooling to Wowza plug-in to Eclipse. For the installation of the Wowza plug-in for Eclipse, the new and debug Wowza project, and the integration of spring, please refer to the once blog. After successful import, the views under Eclipse are as follows:

2. Memcached public Interface Implementation
New Cacheservice interface code such as the following:
Package Com.defonds.wms.module.cache.service;public interface Cacheservice {void init ();//When server start up, do some Thingvoid destory (); While server shut down, do somethingvoid put (String key, Object object); Set new value to Cachevoid remove (String key); Remove from CacheObject get (String key); Get current value from Cacheboolean exist (String key); Check whether the key already exist in cache or not}

Cacheservice Implementation Class Cacheserviceimpl code:
Package Com.defonds.wms.module.cache.service;import Java.io.ioexception;import Java.net.inetsocketaddress;import Net.spy.memcached.memcachedclient;import Com.wowza.wms.logging.wmslogger;import Com.wowza.wms.logging.wmsloggerfactory;public class Cacheserviceimpl implements Cacheservice {private static final Wmslogger logger = wmsloggerfactory.getinstance (). Getloggerobj (CacheServiceImpl.class.getName ());p rivate Memcachedclient memcachedclient = null;private String memcachedipaddress; memcached server IP addressprivate Integer memcachedserverport; memcached server portprivate Integer memcachedobjectexpiration; Objects set to memcached would be expired after 86400 (86400 = $ *) seconds@overridepublic void init () {logger. Debug ("cacheserviceimpl--init--debug--memcachedipaddress=" + this.memcachedipaddress + "; memcachedserverport=" + this.memcachedserverport+ "; memcachedobjectexpiration=" + this.memcachedobjectexpiration); try {logger.debug (" Cacheserviceimpl--init--debug--memcAchedclient would init now "); this.memcachedclient = new Memcachedclient (new Inetsocketaddress (This.memcachedipaddress, This.memcachedserverport));} catch (IOException e) {logger.error (E.getmessage (), E);}} @Overridepublic void Destory () {This.memcachedClient.shutdown ();} @Overridepublic void put (String key, Object object) {try {this.memcachedClient.set (key, This.memcachedobjectexpiration , object);} catch (Exception e) {logger.error ("cacheserviceimpl--put--Insert object into memcached failed!");}} @Overridepublic void Remove (String key) {try {this.memcachedClient.delete (key);} catch (Exception e) {logger.error (" cacheserviceimpl--remove--Delete object from memcached failed! ");}} @Overridepublic object Get (String key) {Object object = Null;try {object = This.memcachedClient.get (key);} catch (Exceptio n e) {logger.error ("cacheserviceimpl--get--Select object from memcached failed!");} return object;} @Overridepublic Boolean exist (String key) {Object object = Null;try {object = This.memcachedclieNt.get (key);} catch (Exception e) {logger.error ("cacheserviceimpl--exist--Select object from memcached failed!");} if (object = = null) {return false;} else {return true;}} public void setmemcachedipaddress (String memcachedipaddress) {this.memcachedipaddress = memcachedipaddress;} public void Setmemcachedserverport (Integer memcachedserverport) {this.memcachedserverport = Memcachedserverport;} public void Setmemcachedobjectexpiration (Integer memcachedobjectexpiration) {this.memcachedobjectexpiration = Memcachedobjectexpiration;}}

3. Increase spymemcached Support
Add Spymemcached-2.8.4.jar to Defonds-server-module's compilation environment and execution environment (the execution environment is%wowza%/lib).
4. Configure Memcached
Is the configuration of the Memcached server address, port number, and data freshness period. Add the following in the Config.properties:
Memcached.ip.address=172.21.0.117memcached.server.port=12111#objects set to memcached would be expired after 86400 ( 86400 = * * secondsmemcached.object.expiration=86400

5. Manage Cacheservice with spring
We entrust the life cycle of the Cacheservice object to spring for management. Add the following configuration under the Beans tab in Spring-context.xml:
<!--cache related--><bean id= "Cacheservice" class= "Com.defonds.wms.module.cache.service.CacheServiceImpl "Init-method=" Init "><property name=" memcachedipaddress "value=" ${memcached.ip.address} "></property ><property name= "Memcachedserverport" value= "${memcached.server.port}" ></property><property Name= "Memcachedobjectexpiration" value= "${memcached.object.expiration}" ></property></bean>

6. Deploy and Debug
Using the text editor, open the Server.xml in the Conf folder under the Wowza installation folder and add the following under the Serverlisteners tab:
<ServerListener> <baseclass>com.defonds.wms.module.server.defondswowzaserverlistener</baseclass > </ServerListener>

Add, for example, the following code in whatever plug-in class:
ApplicationContext appctx = Applicationcontextutils.getapplicationcontext ();  Test cachestring Testflag = "Now" + System.currenttimemillis (); Cacheservice Cacheservice = (cacheservice) appctx.getbean ("Cacheservice"), if (!cacheservice.exist ("Somekey")) { Cacheservice.put ("Somekey", Testflag);} Logger.debug ("defondswowzaserverlistener-onserverinit-memcached-somekey=" + cacheservice.get ("Somekey"));

After executing the plug-in class, printing takes the following example:
DEBUG Server comment-defondswowzaserverlistener-onserverinit-memcached-somekey=now1398324763217
Test success.


Note: Log for the Wowza plugin
The logging implementation of Wowza is an Apache log4j logging tool. log4j configuration file in%wowza%/conf/log4j.properties, default configuration such as the following:

Log4j.rootcategory=info, stdout, serveraccess, Servererror

Suppose we want to change the log level in some of our own definition classes in the plug-in project at development time, only to change it in%wowza%/conf/log4j.properties, and add our own definition log4j.properties Wowza in other places is not recognized. For example, we want to change the log level of Cacheserviceimpl to DEBUG. Then you should add a line at the end of the%wowza%/conf/log4j.properties file:
Log4j.logger.com.defonds.wms.module.cache.service=debug

And then restart Wowza to do it.
In addition, Cacheserviceimpl to get Wmslogger must use this way:
        Wmslogger logger = wmsloggerfactory.getinstance (). Getloggerobj (CacheServiceImpl.class.getName ());

Other, such as Wmsloggerfactory.getlogger (Cacheserviceimpl.class) or Wmsloggerfactory.getlogger (null), can also output logs. However, log4j.logger.com.defonds.wms.module.cache.service=debug changes to its log level are not valid.
PostScript
This resource source code has uploaded csdn resources. Interested friends can go to download, link address: http://download.csdn.net/detail/defonds/7245973.

Integration of Memcached and Wowza projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.