首页 \ 问答 \ 使用guava AbstractScheduledService(Using guava AbstractScheduledService)

使用guava AbstractScheduledService(Using guava AbstractScheduledService)

我正在尝试使用guava AbstractScheduledService定期执行一些任务:

public class MyService extends AbstractScheduledService {

    public MyService() {

    }

    @Override
    protected void runOneIteration() {
        doStuff();
    }

    private void doStuff() {
    // Do stuff
    }

    @Override
    protected Scheduler scheduler() {
        return Scheduler.newFixedRateSchedule(0, 8, TimeUnit.HOURS);
    }

}

所以这项服务应该每8小时定期执行一些任务,但实际上并不是这样。 继承的isRunning()方法返回false,并且永远不会调用runOneIteration()方法。

我已经设法通过从我的服务构造函数调用startAsync()方法(从父类继承startAsync()来使它工作,但我没有看到任何引用说这是应该工作的方式。

我错过了什么吗? 这是AbstractScheduledService工作方式吗?


I'm trying to execute some task periodically using guava AbstractScheduledService :

public class MyService extends AbstractScheduledService {

    public MyService() {

    }

    @Override
    protected void runOneIteration() {
        doStuff();
    }

    private void doStuff() {
    // Do stuff
    }

    @Override
    protected Scheduler scheduler() {
        return Scheduler.newFixedRateSchedule(0, 8, TimeUnit.HOURS);
    }

}

So this service should execute some task periodically every 8 hours but it never actually does. The inherited isRunning() method returns false and the runOneIteration() method never gets invoked.

I have managed to make it work by calling the startAsync() method (inherited from parent class) from my service constructor but I don't see any reference saying this is the way it should work.

Have I missed something here? Is this the way the AbstractScheduledService works?


原文:https://stackoverflow.com/questions/25499811
更新时间:2023-07-12 21:07

最满意答案

是的,不幸的是,这需要一些基于反思的黑客才能实现。 这里是一个示例扩展类:

PropertyGridExtensionHacks.cs

using System.Reflection;
using System.Windows.Forms;

namespace PropertyGridExtensionHacks
{
    public static class PropertyGridExtensions
    {
        /// <summary>
        /// Gets the (private) PropertyGridView instance.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <returns>The PropertyGridView instance.</returns>
        private static object GetPropertyGridView(PropertyGrid propertyGrid)
        { 
            //private PropertyGridView GetPropertyGridView();
            //PropertyGridView is an internal class...
            MethodInfo methodInfo = typeof(PropertyGrid).GetMethod("GetPropertyGridView", BindingFlags.NonPublic | BindingFlags.Instance);
            return methodInfo.Invoke(propertyGrid, new object[] {});
        }

        /// <summary>
        /// Gets the width of the left column.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <returns>
        /// The width of the left column.
        /// </returns>
        public static int GetInternalLabelWidth(this PropertyGrid propertyGrid)
        {
            //System.Windows.Forms.PropertyGridInternal.PropertyGridView
            object gridView = GetPropertyGridView(propertyGrid);

            //protected int InternalLabelWidth
            PropertyInfo propInfo = gridView.GetType().GetProperty("InternalLabelWidth", BindingFlags.NonPublic | BindingFlags.Instance);
            return (int)propInfo.GetValue(gridView);
        }

        /// <summary>
        /// Moves the splitter to the supplied horizontal position.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <param name="xpos">The horizontal position.</param>
        public static void MoveSplitterTo(this PropertyGrid propertyGrid, int xpos)
        {
            //System.Windows.Forms.PropertyGridInternal.PropertyGridView
            object gridView = GetPropertyGridView(propertyGrid);

            //private void MoveSplitterTo(int xpos);
            MethodInfo methodInfo = gridView.GetType().GetMethod("MoveSplitterTo", BindingFlags.NonPublic | BindingFlags.Instance);
            methodInfo.Invoke(gridView, new object[] { xpos });
        }
    }
}

要移动拆分器位置,请使用MoveSplitterTo扩展方法。 使用GetInternalLabelWidth扩展方法来获取分离器的实际位置。 请注意,我观察到,直到SelectedObject被分配并且PropertyGrid未显示,GetInternalLabelWidth返回(-1)。

使用示例:

using PropertyGridExtensionHacks;
//...

    private void buttonMoveSplitter_Click(object sender, EventArgs e)
    {
        int splitterPosition = this.propertyGrid1.GetInternalLabelWidth();
        this.propertyGrid1.MoveSplitterTo(splitterPosition + 10);
    }

Yes, unfortunately this requires some reflection based hacks in order to be achieved. Here is a sample extensions class:

PropertyGridExtensionHacks.cs

using System.Reflection;
using System.Windows.Forms;

namespace PropertyGridExtensionHacks
{
    public static class PropertyGridExtensions
    {
        /// <summary>
        /// Gets the (private) PropertyGridView instance.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <returns>The PropertyGridView instance.</returns>
        private static object GetPropertyGridView(PropertyGrid propertyGrid)
        { 
            //private PropertyGridView GetPropertyGridView();
            //PropertyGridView is an internal class...
            MethodInfo methodInfo = typeof(PropertyGrid).GetMethod("GetPropertyGridView", BindingFlags.NonPublic | BindingFlags.Instance);
            return methodInfo.Invoke(propertyGrid, new object[] {});
        }

        /// <summary>
        /// Gets the width of the left column.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <returns>
        /// The width of the left column.
        /// </returns>
        public static int GetInternalLabelWidth(this PropertyGrid propertyGrid)
        {
            //System.Windows.Forms.PropertyGridInternal.PropertyGridView
            object gridView = GetPropertyGridView(propertyGrid);

            //protected int InternalLabelWidth
            PropertyInfo propInfo = gridView.GetType().GetProperty("InternalLabelWidth", BindingFlags.NonPublic | BindingFlags.Instance);
            return (int)propInfo.GetValue(gridView);
        }

        /// <summary>
        /// Moves the splitter to the supplied horizontal position.
        /// </summary>
        /// <param name="propertyGrid">The property grid.</param>
        /// <param name="xpos">The horizontal position.</param>
        public static void MoveSplitterTo(this PropertyGrid propertyGrid, int xpos)
        {
            //System.Windows.Forms.PropertyGridInternal.PropertyGridView
            object gridView = GetPropertyGridView(propertyGrid);

            //private void MoveSplitterTo(int xpos);
            MethodInfo methodInfo = gridView.GetType().GetMethod("MoveSplitterTo", BindingFlags.NonPublic | BindingFlags.Instance);
            methodInfo.Invoke(gridView, new object[] { xpos });
        }
    }
}

To move the splitter position use the MoveSplitterTo extension method. Use the GetInternalLabelWidth extension method to get the actual position of the splitter. Please note that I observed that until the SelectedObject is assigned and the PropertyGrid was not shown, GetInternalLabelWidth returns (-1).

Sample use:

using PropertyGridExtensionHacks;
//...

    private void buttonMoveSplitter_Click(object sender, EventArgs e)
    {
        int splitterPosition = this.propertyGrid1.GetInternalLabelWidth();
        this.propertyGrid1.MoveSplitterTo(splitterPosition + 10);
    }

相关问答

更多
  • 你有没有尝试过使用表格环境? 下面是一些代码,用于为上面给出的文本创建一条垂直线, \begin{tabular}{|p{10cm}} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id hendrerit nunc. Sed scelerisque lacus vitae erat eleifend eleifend. Donec eros mi, placerat in porta eleifend, placerat a ...
  • 你要找的是你的ComboBox上的HorizontalContentAlignment 。 默认情况下,它设置为Left ,因此您只需将其更改为Stretch 。 这将确保ComboBox中的Content将延伸到ComboBox的最大边界而不是内容的宽度。 另外,你的WrapPanel更适合作为DockPanel或Grid ,因为WrapPanel本质上会将它的内容对齐到左边。 ...
  • 此代码基于代码项目中的一篇文章( http://www.codeproject.com/KB/grid/GridDescriptionHeight.aspx ),其中介绍了两个修复程序和一些清理。 private void ResizeDescriptionArea(PropertyGrid grid, int lines) { try { var info = grid.GetType().GetProperty("Controls"); var colle ...
  • 你可以用几种方法做到这一点 Highchart有一个很酷的渲染器 ,可以让你在图表中添加各种图形。 其中一个选项是添加一条路径,我将在此处说明相同的路径 。 我们将重复使用十字准线的路径,并将其添加到图表中,并添加一些其他样式,例如您提到的颜色。 十字线的路径可以被视为this.tooltip.crosshairs[0].d这是字符串形式,可以使用Array.split()函数轻松地转换为数组 click: function() { this.renderer.path(this.tooltip.c ...
  • 您的DIV规则适用于每个DIV,包括紧挨BODY和DIV.vert 。 因此,应用完整黑色边框的CSS中的第一个DIV规则适用于代码中的每个DIV。 我假设你只想把它应用到顶级DIV而不是一切。 所以给它自己的顶级DIV ,并更新第一个使用类名的规则。 这样,您想要红色左侧行的DIV.vert框也不会获取其他CSS规则。 更新的HTML:
    This Website
    我担心设置高度100%绝对位置只会保留文件高度而不是整页高度的高度。 要设置整页的100%高度,您需要使用固定位置。 但仍然担心会解决你的问题。 由于使用固定定位可能需要为您的布局付出更多努力。 I am afraid setting height 100% with absolute position will only reserve the height as of the document height not whole page height. To set 100% height as of ...
  • 问题是,你的日期( "2001-04-01" )没有正确转换。 您可以调试此类问题,例如使用 > class("2001-04-01") [1] "character" 它不符合你的类从data.frame (这是Date ) >str(datt) 'data.frame': 900 obs. of 3 variables: $ time1 : Date, format: "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" ... $ va ...
  • 我设计了宽度窄,高度高的元素。 但是,从
    创建一条垂直线似乎是非语义的,因此您可能希望使用或其他元素。 body { background: #f4f4f4; } hr.fancy-line { border: 0; height: 180px; width: 5px; margin: 0 auto; background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.1) 0%, rgba( ...
  • 是的,不幸的是,这需要一些基于反思的黑客才能实现。 这里是一个示例扩展类: PropertyGridExtensionHacks.cs using System.Reflection; using System.Windows.Forms; namespace PropertyGridExtensionHacks { public static class PropertyGridExtensions { /// /// Gets the ...
  • 我猜是因为你的问题不够明确: omega[a_] := 2 Pi/a^(3/2); ListPlot[Flatten[ Table[{a /. FindRoot[omega[a]/omega[5.2] == j/i + 1, {a, 1}], i + j}, {j, 1, 7, 1}, {i, 1, 7, 1}], 1], Filling -> Axis, PlotRange -> {{0, 6}, {0, 15}}] I'm part guessing, beca ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)