小编use*_*180的帖子

Dropwizard ScheduledExecutorService

在我的情况下,我需要运行一些计划任务(例如每分钟)在DB中进行一些检查,如果需要,还需要一些子任务.这应该是没有DB健康检查!

DW文档说:

"应该注意,Environment有针对ExecutorService和ScheduledExecutorService实例的内置工厂方法.有关详细信息,请参阅LifecycleEnvironment#executorService和LifecycleEnvironment#scheduledExecutorService."

有谁知道如何在DW中实现这一点?试着玩DW代码的可能性,我发现了这个:

String nameFormat = "?What should this string contain?";
ScheduledExecutorServiceBuilder sesBuilder = environment.lifecycle().scheduledExecutorService(nameFormat);
ScheduledExecutorService ses = sesBuilder.build();
Runnable alarmTask = new AlarmTask();
ses.scheduleWithFixedDelay(alarmTask, 0, 5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

这是DW中正确的方法吗?BTW一个可运行的假人:

  private static final class AlarmTask implements Runnable {
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Calendar cal = Calendar.getInstance();
        @Override public void run() {
          ++fCount;

            cal = Calendar.getInstance();
            System.out.println(fCount + "x BEEP:" + dateFormat.format(cal.getTime()));
        }
        private int fCount;
      }
Run Code Online (Sandbox Code Playgroud)

什么是初始名称的目的,是否在某处使用?希望有人能提供帮助.

java scheduled-tasks dropwizard

14
推荐指数
1
解决办法
6803
查看次数

如何获取与数组类型相关联的类?

有谁知道写下面语句的更好方法:

example.mySpecialMethod(new MySpecialClass[0].getClass())
Run Code Online (Sandbox Code Playgroud)

我需要数组类型,但我不知道是否有更好的解决方案.目前这个例子适合我,但也许有人知道一个更好的方法来做同样没有new关键字.

java arrays class new-operator

13
推荐指数
1
解决办法
507
查看次数

使用TestResource进行Dropwizard集成测试

有谁知道如何添加测试资源(即仅用于测试目的而不是在应用程序的run()方法中添加的测试资源)?

这是一个例子:

public class MyTest {   
    @ClassRule
    public static final DropwizardAppRule<TestConfiguration> RULE =
            new DropwizardAppRule<TestConfiguration>(MyApp.class, "my-app-config.yaml");


    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        MyTest.RULE.getEnvironment().jersey().register(new JustForTestingResource());
    }


    @Test
    public final void testTestResource()
    {
        Client client = new Client();

        ClientResponse response = client.resource(
            String.format("http://localhost:%d/rest/v1/test", RULE.getLocalPort()))
            .get(ClientResponse.class);

        assertThat(response.getStatus(), is(200));   
    }
}
Run Code Online (Sandbox Code Playgroud)

public class JustForTestingRessource {


    @GET
    @Path("test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getInTestResource()
    {
        return Response.status(Status.OK).type(MediaType.TEXT_PLAIN).entity("get @Path(\"test\") is ok").build();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是添加的资​​源没有添加,我找不到资源404错误响应.看来我在资源发布后注册了新资源,启动后Dropwizard里面没有刷新.

我不想扩展我的Application类,我不想将测试代码插入到我的实际应用程序代码中.有没有人知道如何注册测试资源而不在应用程序的run()方法中注册它?

这有效,但需要一个新类:

public class TestService extends MyService{


    @Override
    public void run( …
Run Code Online (Sandbox Code Playgroud)

java testing rest dropwizard

6
推荐指数
1
解决办法
1万
查看次数

Dropwizard路径:获取应用程序中的所有路径

有谁知道如何获得有关使用什么路径的相同信息,例如在dw应用程序启动时。我的意思是此行之后的输出:

io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET     /path/of/res/test (this.is.the.class.package.info.MyRessource)
POST     /path/of/res/test2 (this.is.the.class.package.info.MyRessource2)
Run Code Online (Sandbox Code Playgroud)

我必须检查是否存在特定路径。

java dropwizard

5
推荐指数
1
解决办法
3251
查看次数

Dropwizard digest auth

Dropwizard是否也支持摘要认证?我发现的只是基本身份验证和OAuth.这个示例代码很不错.

如果确实没有摘要支持并且已经存在exisitng代码,那么在DW中进行摘要身份验证最好的想法是什么?

实现过滤器将终止非auth所需资源的使用.

到目前为止我发现了什么:

java authentication digest-authentication dropwizard

5
推荐指数
1
解决办法
421
查看次数

在java代码中获取Logback模式

我通过 MDC 插入一些变量,但如果此变量未在 logback 模式中使用,我想记录一个警告。有谁知道如何在java代码中检索logback模式?某事。像这样:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
...

...
Logger LOG = LoggerFactory.getLogger(MyTestClass.class);
...

MDC.put("id","thisIsATestId");
String pattern = LOG.getLogbackPattern();
checkPatternAndWarnIfMissingVar(pattern,"id");
...
Run Code Online (Sandbox Code Playgroud)

java logging logback

4
推荐指数
1
解决办法
2036
查看次数

Java和Apache客户端的摘要身份验证:始终为401未经授权

我正在尝试使用HTTP客户端实现摘要身份验证,但是目前无法正常工作。

有人可以检查此代码是否正确吗?出于测试目的,我使用http://httpbin.org/,但我得到的只是HTTP/1.1 401 Unauthorized

这是示例代码:

private static void doDigestAuth() throws ClientProtocolException,
    IOException,
    AuthenticationException,
    MalformedChallengeException
{

    HttpHost target = new HttpHost("httpbin.org", 80, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(
        "user", "passwd"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {

        HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();

        // …
Run Code Online (Sandbox Code Playgroud)

java digest-authentication apache-httpclient-4.x

4
推荐指数
1
解决办法
1173
查看次数

2
推荐指数
1
解决办法
257
查看次数