小编Lac*_*lev的帖子

"关闭"阻塞队列

我在一个非常简单的生产者 - 消费者场景中使用java.util.concurrent.BlockingQueue.例如,这个伪代码描述了消费者部分:

class QueueConsumer implements Runnable {

    @Override
    public void run() {
        while(true)
        {
            try {
                ComplexObject complexObject = myBlockingQueue.take();
                //do something with the complex object
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.在阻塞队列的javadoc中,我读到:

BlockingQueue本质上不支持任何类型的"关闭"或"关闭"操作,以指示不再添加任何项目.这些功能的需求和使用倾向于依赖于实现.例如,一种常见的策略是生产者插入特殊的流末端或毒物对象,这些对象在被消费者采用时会相应地进行解释.

不幸的是,由于使用的泛型和ComplexObject的性质,将"毒物对象"推入队列并非易事.所以这个"常用策略"在我的场景中并不是很方便.

我的问题是:我可以用什么其他好的策略/模式来"关闭"队列?

谢谢!

java multithreading blockingqueue

27
推荐指数
4
解决办法
2万
查看次数

How to disable Eureka and Spring Cloud Config in a WebMvcTest?

I play with a simple Spring Boot application which registers itself in Eureka and uses spring cloud configuration to read some properties.

Here it goes:

@SpringBootApplication
@EnableEurekaClient
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

In bootstrap.yml I have:

eureka:
    client:
        serviceUrl:
            defaultZone: http://registry-service:5051/eureka/

spring:
    application:
        name: product-service
    cloud:
        config:
            discovery:
                serviceId: config-service
                enabled: true
            fail-fast: true
            retry:
                initial-interval: 2000
Run Code Online (Sandbox Code Playgroud)

I have a rest controller for which I want to write a …

spring-test spring-boot spring-cloud-netflix spring-cloud-config

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

OAuth 2.0和Azure Active Directory - 错误AADSTS90009

我正在尝试使用OAuth 2.0和Azure AD授权访问我们的Web应用程序.在这里指导.

用户被重定向到类似的URL:

https://login.microsoftonline.com/common/oauth2/authorize?
    client_id=d220846b-1916-48d2-888b-9e16f6d9848b&
    response_type=code&
    response_mode=query&
    state=[secure-random]&
    redirect_uri=[my_uri]&
    resource=[my app ID uri taken from app settings]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

AADSTS90009:应用程序'd220846b-1916-48d2-888b-9e16f6d9848b'正在为自己请求令牌.仅当使用基于GUID的应用程序标识符指定资源时,才支持此方案.

这个描述并没有真正帮助我.我检查了这个帖子,但我还是迷路了.

这个错误是什么意思,哪个是基于GUID的应用程序标识符?资源的价值应该如何?非常感谢.

azure oauth-2.0 azure-active-directory

12
推荐指数
1
解决办法
7255
查看次数

关闭ZipOutputStream

我有点困惑.我知道空拉链不合法.但是这个示例片段呢:

ZipOutputStream zos = null; 
try
{
    zos = new ZipOutputStream(new FileOutputStream("..."));
    //
    //..
    //
}
finally
{
    zos.close();
}
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因(可能是异常情况)没有添加任何zip条目,则在紧密尝试时将抛出以下异常:

Exception in thread "main" java.util.zip.ZipException: ZIP file must have at least one entry
    at java.util.zip.ZipOutputStream.finish(ZipOutputStream.java:304)
    at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:146)
    at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:321)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,关闭流的最简洁方法是什么?

谢谢...

java zip

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

java.util.zip - ZipInputStream vs ZipFile

我对java.util.zip图书馆有一些一般性的问题.我们基本上做的是导入和导出许多小组件.以前,这些组件是使用单个大文件导入和导出的,例如:

<component-type-a id="1"/>
<component-type-a id="2"/>
<component-type-a id="N"/>

<component-type-b id="1"/>
<component-type-b id="2"/>
<component-type-b id="N"/>
Run Code Online (Sandbox Code Playgroud)

请注意,导入期间组件的顺序是相关的.

现在每个组件都应该占用自己的文件,该文件应该是外部版本的,QA-ed,bla,bla.我们决定导出的输出应该是一个zip文件(包含所有这些文件),我们导入的输入应该是一个类似的zip文件.我们不想在我们的系统中爆炸zip.我们不希望为每个小文件打开单独的流.我目前的问题:

Q1.可以ZipInputStream保证zip条目(小文件)的读取顺序与我们使用的导出插入的顺序相同ZipOutputStream吗?我认为阅读是这样的:


ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) 
{
       //read from zis until available
}
Run Code Online (Sandbox Code Playgroud)

我知道中央zip目录放在zip文件的末尾但是内部的文件条目有顺序.我也知道依靠订单是一个丑陋的想法,但我只想记住所有的事实.

Q2.如果我使用ZipFile(我更喜欢),呼叫getInputStream()数百次会对性能产生什么影响?它会比ZipInputStream解决方案慢得多吗?拉链只打开一次ZipFile并由后备RandomAccessFile- 这是正确的吗?我认为阅读是这样的:


ZipFile zipfile = new ZipFile(argv[0]);
Enumeration e = zipfile.entries();//TODO: assure the order of the entries
while(e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();
        is = …
Run Code Online (Sandbox Code Playgroud)

java io zip thread-safety

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

Month和java.util.Formatter

java.util.Formatterjavadoc中,我读到:

'm'月,格式化为两位数,必要时带前导零,即01 - 13.

为什么13?

java calendar date

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

MVVM-使用RxJava和Room在ViewModel中处理Disposable-s

我尝试在MV活动中应用MVVM模式(我是Android菜鸟)。

我将Room与RxJava 2一起使用,例如,这是我的存储库中方法的签名:

public Single<MissionTask> getMissionTaskByID(long id) {..}
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel类中,我对存储库和代码有如下引用:

private void doSomethingOnUserEvent() {
    ...
      missionTaskRepository.getMissionTaskByID(firstID).
          observeOn(AndroidSchedulers.mainThread()).
          subscribeOn(Schedulers.io()).
          subscribe(missionTask ->
              {
                // do some work and update live data
              },
              t -> {
                // handle error
              });
    ...
  }
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切似乎都很好。现在- subscribe返回Disposable

我的问题是:

  1. 我应该如何处理一次性用品(例如,可以将其放入复合一次性用品中并在清除模型后将其丢弃)?
  2. 如果我不处理该怎么办?泄漏?为什么?

在我经历的一些示例中,没有处理Disposable。

更新:我已经在android-architecture-components中看到了复合一次性材料的用法。

谢谢。

android mvvm rx-java2 android-room android-viewmodel

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

春季启动:排除基于环境变量的自动配置

在春季启动中,我们可能会排除特定的自动配置类,这样就永远不会应用它们。基于注释的配置示例:

@SpringBootApplication(exclude = OneConfiguration.class)
Run Code Online (Sandbox Code Playgroud)

我想要为环境变量提供一个值,该值将确定一些将被排除的自动配置。有什么想法要实现吗?

java spring spring-boot

5
推荐指数
2
解决办法
3511
查看次数

注册后自动登录 Spring Boot 3/Spring security 6

我的问题主要针对 Spring Boot 3.x/Spring Security 6.x,但也可能适用于旧版本。我有一个带有表单登录的小项目。这是一个示例配置。

@Configuration
public class SecurityConfig {

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.
        ...
        formLogin().
        loginPage("/users/login").
        ...
    return http.build();
  }

  @Bean
  public UserDetailsService userDetailsService(UserRepository userRepository) {
    return new AppUserDetailsService(userRepository);
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
}
Run Code Online (Sandbox Code Playgroud)

确实没什么特别的,而且工作得很好。我还有一张登记表。它也工作正常,并且用户已在数据库中正确创建。之后就可以登录了。到目前为止,一切都很好。

我想做的最后一件事是在注册后立即自动登录用户。在早期的 Srping 版本中,这使用了类似的代码,可能是受到这里的启发:

@Override
  public void createAccount(UserRegistrationDTO userRegistrationDTO) {

    UserEntity userEntity = new UserEntity();
    userEntity.
        setFirstName(userRegistrationDTO.getFirstName()).
        setLastName(userRegistrationDTO.getLastName()).
        setEmail(userRegistrationDTO.getEmail()).
        setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
    userRepository.save(userEntity);

    var userDetails = userDetailsService.loadUserByUsername(userRegistrationDTO.getEmail());
    Authentication authentication = new UsernamePasswordAuthenticationToken(
      userDetails, …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-boot

5
推荐指数
2
解决办法
2678
查看次数

在Guice中使用命名注入

我正在使用Guice进行依赖项注入,我有些困惑。Named不同的程序包中有两个注释:

com.google.inject.name.Namedjavax.inject.Named(JSR 330?)。

我渴望依靠javax.inject.*。代码示例:

import javax.inject.Inject;
import javax.inject.Named;

public class MyClass
{
    @Inject
    @Named("APrefix_CustomerTypeProvider")
    private CustomerTypeProvider customerTypeProvider;
}
Run Code Online (Sandbox Code Playgroud)

在命名模块中,我可能有以下一行:

bind(CustomerTypeProvider.class).annotatedWith(...).toProvider(CustomerTypeProviderProvider.class);
Run Code Online (Sandbox Code Playgroud)

问题:我很好奇应该把点放在哪里?我会期望类似的东西,com.google.inject.name.Names.named("APrefix_CustomerTypeProvider")但是com.google.inject.name.Named当我需要一个的时候,这个会返回javax.inject

CustomerTypeProviderProvider.class.getAnnotation(javax.inject.Named.class)也不太适合,因为CustomerTypeProviderProvider(未注释愚蠢的名称,遗留问题)未添加注释。

java dependency-injection guice

3
推荐指数
1
解决办法
1602
查看次数

通过构造函数注入和使用 Guice 通过字段注释注入有什么区别?

我有下面的代码使用 Guice 进行依赖注入。第一个是使用构造函数注入,而另一个是@Inject直接在字段上方添加。这两种方式有什么区别吗?Guice官网上好像推荐构造函数注入。

class BillingService {
    private final CreditCardProcessor processor;
    private final TransactionLog transactionLog;

    @Inject
    BillingService(CreditCardProcessor processor, TransactionLog transactionLog) {
        this.processor = processor;
        this.transactionLog = transactionLog;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

和:

class BillingService {
    @Inject
    private final CreditCardProcessor processor;
    @Inject
    private final TransactionLog transactionLog;
    BillingService() {

    }
    ...       
}
Run Code Online (Sandbox Code Playgroud)

dependency-injection guice

3
推荐指数
1
解决办法
97
查看次数

Jenkins-将脚本化管道导出到共享库中

我们有一些类似的应用程序已通过脚本化管道进行部署,该管道基本上是所有应用程序的C&P。我想将整个管道移到Jenkins docs所暗示的Jenkins共享库中。

因此,假设我的代码中包含以下“管道” var/standardSpringPipeline.groovy

#!groovy

def call() {

  node {
    echo "${env.BRANCH_NAME}"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后-Jenkins文件:

@Library('my-jenkins-lib@master') _

standardSpringPipeline

echo "Bye!"
Run Code Online (Sandbox Code Playgroud)

不幸的是,由于我不了解的原因,这无法正常工作。Jenkins的输出类似:

> git fetch --no-tags --progress ssh://git@***.com:7999/log/my-jenkins-lib.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 28900d4ed5bcece9451655f6f1b9a41a76256629 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 28900d4ed5bcece9451655f6f1b9a41a76256629
Commit message: "NOJIRA: ...."
 > git rev-list --no-walk 28900d4ed5bcece9451655f6f1b9a41a76256629 # timeout=10
[Pipeline] echo
Bye!
[Pipeline] End of Pipeline
Run Code Online (Sandbox Code Playgroud)

有什么线索为什么这不起作用(请参见上面的输出),什么是正确的方法?

groovy jenkins jenkins-pipeline

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

角度,材质侧面导航和粘性工具栏

我正在尝试侧面导航和Angular 6/7。接下来的两张图片显示了我想要实现的目标。

侧面导航已折叠的应用程序:

在此处输入图片说明

侧面导航扩展的应用程序:

在此处输入图片说明

简而言之:

  • 可以通过按钮打开和关闭的侧面导航。
  • 一个固定的工具栏的内容的上方。
  • 可滚动的内容。

其基本结构如下,暗示这里

<mat-sidenav-container>
  <mat-sidenav mode="side" opened>Sidenav content</mat-sidenav>
  <mat-sidenav-content>Main content, toolbar here</mat-sidenav-content>
</mat-sidenav-container>
Run Code Online (Sandbox Code Playgroud)

一个最小的工作样本,可以发现在这里,在stackblitz。

我的问题:工具栏不粘滞,在我开始滚动时会与内容一起滚动。

我的问题:如何使工具栏停留在顶部而不与内容一起滚动?

注意:侧面导航本身是固定的,因为它具有fixedInViewport="true"

material-design angular-material angular

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