Skip to content

Commit

Permalink
增加对目录监控的基本功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalW committed Nov 25, 2009
1 parent 6dd0e4a commit 5ea8ddb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 21 deletions.
5 changes: 1 addition & 4 deletions vp/vp-backend/bin/VideoConvertor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class Convertor implements MessageListener{

private static Logger logger=Logger.getLogger(Convertor.class)

static{
PropertyConfigurator.configure("log4j.properties");
}

def connection
def session

Expand Down Expand Up @@ -54,4 +50,5 @@ class Convertor implements MessageListener{
}
}

PropertyConfigurator.configure("log4j.properties");
new Convertor().start()
103 changes: 102 additions & 1 deletion vp/vp-backend/bin/VideoDirectoryMonitor.groovy
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
#!/usr/bin/env groovy
#!/usr/bin/env groovy

import java.util.Arrays;

import groovy.lang.Grab;
import org.apache.log4j.*
import javax.jms.*
import org.apache.activemq.ActiveMQConnectionFactory
import com.lightspeedleader.directorywatcher.*

PropertyConfigurator.configure("log4j.properties");

@Grab(group="com.lightspeedleader",module="directorywatcher",version="1.0")
@Grab(group="log4j",module="log4j",version="1.2.12")
@Grab(group="org.apache.activemq",module="activemq-core",version="5.3.0")
class Monitor extends BaseListener implements IFileListener{

private static Logger logger=Logger.getLogger(Monitor.class)

def videoExtends=['mov','flv','avi'] as Set

def connection

public void onStart(Object monitoredResource) {
logger.debug("开始监控目录资源,目录为:${monitoredResource}")
def connectionFactory=new ActiveMQConnectionFactory("tcp://localhost:61616")
connection=connectionFactory.createConnection()
connection.start()
}

public void onStop(Object notMonitoredResource) {
logger.debug("停止监控目录资源,目录为:${notMonitoredResource}")
connection.close()
}

public void onAdd(Object newResource) {
logger.debug("监控目录增加了新资源,资源名称:${newResource}")

if(!isVideoFile(newResource)){
logger.debug("资源${newResource}不是视频文件。")
return
}

def session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE)
def destination=session.createQueue("video_convert")
def producer=session.createProducer(destination)
producer.setDeliveryMode(DeliveryMode.PERSISTENT)
def fileName=newResource.name.substring(0,newResource.name.lastIndexOf('.'))
def message=session.createTextMessage("${newResource};${fileName}.mp4")
producer.send(message)
session.commit()
session.close()
logger.debug("已将${newResource}提交到队列${destination}中。")
}

def isVideoFile(file){
if(file.isDirectory()){
return false
}

if(file.name.startsWith('.')){
return false
}

if (videoExtends.contains(file.name.tokenize('.').last().toLowerCase())){
return true
}

false
}

public void onChange(Object changedResource) {
logger.debug("监控目录有资源发生变化,资源名称:${changedResource}")
onAdd(changedResource)
}

public void onDelete(Object deletedResource) {
logger.debug("监控目录有资源被删除,资源名称:${deletedResource}")
}
}

class ShutdownThread extends Thread{

private static final Logger logger=Logger.getLogger(ShutdownThread.class)

def dw

ShutdownThread(dw){
this.dw=dw
}

public void run(){
logger.debug("关闭文件目录监控...")
this.dw.stop()
logger.debug("已关闭。")
}
}

DirectoryWatcher dw = new DirectoryWatcher(".", 5);
dw.addListener(new Monitor());
dw.start();
Runtime.runtime.addShutdownHook(new ShutdownThread(dw))
2 changes: 2 additions & 0 deletions vp/vp-backend/bin/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ log4j.logger.noModule=FATAL
# My business
log4j.logger.com.easymorse=DEBUG
log4j.logger.Convertor=DEBUG
log4j.logger.Monitor=DEBUG
log4j.logger.ShutdownThread=DEBUG
38 changes: 22 additions & 16 deletions vp/vp-backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>vp</artifactId>
<groupId>com.easymorse</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<groupId>com.easymorse</groupId>
<artifactId>vp-backend</artifactId>
<version>1.0-SNAPSHOT</version>
<name>vp-backend</name>
<dependencies>
</dependencies>
<build>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>vp</artifactId>
<groupId>com.easymorse</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<groupId>com.easymorse</groupId>
<artifactId>vp-backend</artifactId>
<version>1.0-SNAPSHOT</version>
<name>vp-backend</name>
<dependencies>
<dependency>
<groupId>com.lightspeedleader</groupId>
<artifactId>directorywatcher</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>bin</sourceDirectory>
</build>
</project>

0 comments on commit 5ea8ddb

Please sign in to comment.