2014-12-03 50 views
4

我有一个Dropwizard网络服务器,其余的api也提供一些静态内容,如html,css,javascript和jpg图像。不幸的是,当我更改html或添加其他图像时,始终需要重新启动服务器才能将更改生效。在Dropwizard中禁用静态资产缓存

正如我认为这可能是一个缓存问题,我探索了bazaarvoice's Configurable Assets Bundle

这就是我添加到配置类:

@Valid 
@NotNull 
@JsonProperty 
private final AssetsConfiguration assets = new AssetsConfiguration(); 

,并在主类

@Override 
public void initialize(Bootstrap<MyConfiguration> bootstrap) { 
    // ... 
    CacheBuilderSpec cacheBuilderSpec = CacheBuilderSpec.disableCaching(); 
    bootstrap.addBundle(new ConfiguredAssetsBundle("/html", cacheBuilderSpec, "/", "index.html", "Static assets")); 
} 

@Override 
public void run(MyConfiguration config, Environment env) { 
    env.jersey().setUrlPattern("/api/*"); 
    // ... 
} 

在YAML配置没有变化。

静态文件位于的src /主/资源/ HTML。 如何禁用缓存以使Dropwizard立即显示更改?

第二个问题,我如何让Dropwizard跟随资产目录中的符号链接?

更新

我发现这个在ConfiguredAssetsBundle来源:

// Let the cache spec from the configuration override the one specified in the code 
CacheBuilderSpec spec = (config.getCacheSpec() != null) 
    ? CacheBuilderSpec.parse(config.getCacheSpec()) 
    : cacheBuilderSpec; 

这肯定将覆盖这是在代码中设置从YAML文件中配置缓存建设者规范。在追加

assets: 
    cacheSpec: maximumSize=0 

到配置,调试器显示最大大小现在为0.但是,行为没有改变。

回答

1

静态内容不会更改,因为您需要重新启动,但因为运行的服务器实际上为目标目录下的文件提供服务。更改此目录中的文件只会让事情混淆(因此它不是一个真正的解决方案),但改变几行并等待一秒,以验证服务器现在可以为修改的文件提供服务,而无需重新启动。

作为解决方案,我更喜欢在eclipse中打开dropwizard项目作为maven项目,并使用exec-maven-plugin在终端上运行mvn exec:java。 Eclipse会在文件更改时更新目标目录,但这需要几秒钟时间,具体取决于项目的大小。