NServicebus与文件系统观察 [英] NServicebus with File System Watcher

查看:267
本文介绍了NServicebus与文件系统观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的端点发送出去每当检测到特定文件夹的文件被丢弃的事件。我能得到它利用它实现IWantToRunWhenBusStartsAndStops,这反过来又设置了一个FileSystemWatcher的监视给定文件夹的一类的工作。 ?我的问题是,这是去这与nservicebus还是我失去了一些东西,可能会导致我的麻烦了线的最佳方式。

I want my endpoint to send out an event whenever a detects a file in a specific folder is dropped. I was able to get it to work by using a class which implements IWantToRunWhenBusStartsAndStops, which in turn sets up a FileSystemWatcher to monitor the given folder. My questions is, is this the best way to go about this with nservicebus or am I missing something that may cause me trouble down the line?

下面是我的代码:

public class FileSystem : IWantToRunWhenBusStartsAndStops
{
    private FileSystemWatcher watcher;

    public void Start()
    {
        ConfigFileWatcher();
    }

    public void Stop()
    {

    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private void ConfigFileWatcher()
    {
        watcher = new FileSystemWatcher();
        watcher.Path = @"c:\";
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }

    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

        // fire off an event here...
    }

}

推荐答案

如果你看一下NServiceBus的源代码,在容器初始化,你会看到IWantToRunWhenBusStartsAndStops注册与一种全通话

If you look at the NServiceBus source code, in the container initialization, you will see that IWantToRunWhenBusStartsAndStops is registered with the life cycle of one per call

ForAllTypes<IWantToRunWhenBusStartsAndStops>(TypesToScan, t => configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerCall));

这意味着该类会开始后进行处理()被调用。你的实现作品,因为你的事件订阅一个静态的处理程序,这使订阅活着。

This means that the class will be disposed after Start() is called. Your implementation works because your events are subscribed to a static handler, which keeps subscriptions alive.

我们使用的文件观察家生产,但我们烤他们先进的卫星。卫星是保证被初始化为单身,也不会进行处理。他们有启动和停止方法为好。他们确实有地址,应该能够处理传入的消息,但你可以使用一些虚拟地址,什么也不做在处理程序,除非你想使你的文件系统观察卫星双向(即接收消息,并把它们作为文件在磁盘)。

We use file watchers in production but we bake them as advanced satellites. Satellites are guaranteed to be initialized as singletons and will not be disposed. They have Start and Stop methods as well. They do have addresses and should be able to handle incoming messages but you can use some dummy address and do nothing in the handler unless you want to make your file system watcher satellite bidirectional (ie receive messages and put them as files on the disk).

在NServiceBus它推荐,使不断磨合,非一次性的过程为卫星。许多NServiceBus组件制成的卫星。

In NServiceBus it is recommended to make constantly running, non-disposable processes as satellites. Many NServiceBus components are made as satellites.

您可能会好奇如何制作自己的一颗卫星,但它是很容易做到。您可以检查接口签名 rel=\"nofollow\"> 。

You might get curious how to make a satellite yourself, but it is quite easy to do. You can check the interface signature here.

这将是这个样子

using System;
using System.IO;
using NServiceBus;
using NServiceBus.Satellites;

public class FileSystem : ISatellite
{
    private FileSystemWatcher _watcher;

    public bool Handle(TransportMessage message)
    {
        return true;
    }

    public void Start()
    {
        _watcher = new FileSystemWatcher
        {
            Path = @"c:\",
            NotifyFilter = NotifyFilters.LastAccess |    NotifyFilters.LastWrite
                           | NotifyFilters.FileName | NotifyFilters.DirectoryName,
            Filter = "*.txt"
        };
        _watcher.Changed += OnChanged;
        _watcher.Created += OnChanged;
        _watcher.Deleted += OnChanged;
        _watcher.EnableRaisingEvents = true;
    }

    public void Stop()
    {
        _watcher.Dispose();
    }

    public Address InputAddress
    {
        get { return Address.Parse("FileSystemSatellite"); }
    }

    public bool Disabled
    {
        get { return false; }
    }

    // Define the event handlers. 
    private void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or    deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

        // fire off an event here...
    }
}

有一件事你必须记住:每颗卫星都有自己的队列。在这种情况下,它将永远是空的。

There one thing you need to remember: each satellite gets its own queue. In this case it will always be empty.

这篇关于NServicebus与文件系统观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆