小编Mil*_*nov的帖子

保持spring上下文有效,直到消耗JMS消息

我有一个非常标准的设置JMS- Spring BootActiveMQ.它工作正常,直到我尝试进行简单的集成测试.经过一些调查后,我发现在第一个JMS消息被消耗后,Spring上下文和嵌入式代理都被关闭,无论消费过程中是什么,都会触发另一个事件.经纪人问题我能够通过useShutdownHook=false在测试设置中添加连接选项来解决,即

spring.activemq.broker-url = vm://broker?async=false&broker.persistent=false&broker.useShutdownHook=false
Run Code Online (Sandbox Code Playgroud)

我正在寻找的基本上是一种强制测试"保持活力"的方法,直到消耗掉所有JMS消息(在这种情况下它们只是两个).我理解整个设置的异步性质,但是在测试期间,生成和使用这些消息的所有结果会很有帮助.

下面是我的设置,虽然它很简单.

@EnableJms
public class ActiveMqConfig {

    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        jmsTemplate.setMessageConverter(messageConverter);
        return jmsTemplate;
    }

    @Bean
    public MessageConverter messageConverter() {
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
        messageConverter.setTargetType(MessageType.TEXT);
        messageConverter.setTypeIdPropertyName("_type");
        return messageConverter;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个消息驱动的POJO,它监听给定的事件:

@JmsListener(destination = "events")
public void applicationSubmitted(MyType event) {
    // do some work with the event here

    jmsTemplate.convertAndSend("commands", mymessage);
}
Run Code Online (Sandbox Code Playgroud)

还有一个:

@JmsListener(destination = "commands")
public void onCommand(TextMessage …
Run Code Online (Sandbox Code Playgroud)

java activemq-classic jms spring-boot

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

针对不同固定大小项目的角度虚拟滚动策略

我正在使用 Angular 的cdk-virtual-scroll-viewport. 除了订阅视图位置之外,该功能不依赖于它的任何特殊功能,以便在用户滚动到底部时加载新元素(在自定义中DataSource<Item>):

connect(collectionViewer: CollectionViewer): Observable<Item[]> {
  this.viewChanges = collectionViewer.viewChange.subscribe((listRange) => {
    this.loadItemsFor(listRange);
  });
  ..
}
Run Code Online (Sandbox Code Playgroud)

当所有项目都具有相同的高度时(在 css 和 中指定itemSize<cdk-virtual-scroll-viewport>,效果很好。现在我尝试添加不同类型的项目,其大小不同(可以说100pxvs 50px)。这并不与FixSizeVirtualScrollStrategy配合良好,因此我尝试使用autosizecdl-experimental使用AutoSizeVirtualScrollStrategy)。但是,使用动态策略,一旦将新元素添加到备份虚拟滚动的数据源,滚动位置就会闪烁(我假设是因为ItemAverager)。

是否有可行的方法来实施这两种策略的混合?我知道列表中每个项目的类型,因此它的高度,所以应该可以准确计算正在显示的内容和要加载的内容?当然,对于大型集合来说,它的性能可能不那么好。

angular-material angular angular-cdk angular-cdk-virtual-scroll

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

avro schema 有效性和兼容性的本地验证

我们用于avro对流经消息代理的消息进行(反)序列化。为了存储 avro 文件,使用了模式注册表 ( apicurio )。这提供了两个好处 - 模式验证和兼容性验证。但是,我想知道是否有一种方法可以绕过模式注册表并使用脚本/插件在本地实现相同的目的。验证 avro 文件在语法/语义上是否有效应该是可能的。这同样适用于兼容性验证,因为检查新的模式版本是否与其他模式(以前​​的版本)列表向后/向前兼容在本地听起来也是可行的。

有图书馆可以做到这一点吗?理想情况下是 gradle 插件,但 java/python 库也可以,因为它可以轻松地从 gradle 任务调用。

gradle avro

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

如何将角度材料日期选择器中的日期作为字符串绑定到模型?

我们使用标准日期选择器组件,来自 Angular material (v. 9.1.2),如下所示:

<mat-form-field>
    <mat-label i18n>Date of birth</mat-label>
    <input matInput [matDatepicker]="picker" formControlName="dateOfBirth" />
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

日期采用 ISO 格式,例如1979-12-02. 一旦绑定到表单并显示出来,我们就可以通过调用getRawValue整个表单来恢复它。但是,这Date会将日期作为 javascript 返回,然后将其转换为字符串并以“完整”ISO 格式发送到后端,例如1979-12-02TXX:00:00.000Z,这会破坏联系人/API。

如果我们使用MatMomentDateModule代替MatNativeDateModule,我们会返回一个 js 日期(而不是 javascript Date),但这对格式化没有帮助。

有没有办法将控件的原始值绑定为字符串而不是日期?最好不要将组件包裹在ControlValueAccessor? 也许是习俗DateAdapter

angular-material angular

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

没有webpackJsonp的独立自定义Webpack配置

我们有一个标准的Angular 8应用程序,但是由于某些与业务相关的特定原因,我们需要公开一些javascript功能。为了使用一种构建并能够重复使用角度应用程序中的代码,我们使用了一个自定义的webpack配置,如下所示:

"customWebpackConfig": {
  "path": "./webpack.exposed.js",
  "replaceDuplicatePlugins": true,
  "mergeStrategies": {
    "module.rules": "append"
  }
}
Run Code Online (Sandbox Code Playgroud)

的内容webpack.exposed.js

module.exports = {
  entry: {
    'for-others': './src/for-others/for-others.ts',
  },
  output: {
    filename: '[name].js',
  }
};
Run Code Online (Sandbox Code Playgroud)

for-others文件仅包含一个导出的函数:export default () => {'config1': 1, 'config2': 2};

这样效果很好,并生成了一个单独的for-others.js文件。问题在于该文件不仅以某种方式公开了该函数,而且还向全局webpackJsonp数组添加了一些内容。这意味着其他“外部系统”不能使用我们的配置,因为当push对其进行评估时,我们得到一个数字(push实际上返回类型是什么)。

(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["for-others"],{
},[["./src/for-others/for-others.ts","runtime","vendor"]]]);
Run Code Online (Sandbox Code Playgroud)

我们已经在另一个使用单独的Webpack构建的项目中处理了此要求。生成的文件在那里:

/******/ (function(modules) { // webpackBootstrap
/******/ ...
/******/ })({<code>})
Run Code Online (Sandbox Code Playgroud)

在那里,我们使用了一个包装器插件,(() => {\nreturn该插件仅在此代码之前和\n})().default之后添加,因此整个文件将评估为默认导出的函数。

我已经看到了这些 问题, 已经 …

webpack angular

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

自定义http安全配置以及OAuth2资源服务器

我正在使用Spring Security OAuth2一个非常基本的配置,工作正常.我现在想要一个WebSecurityConfigurerAdapter包含自定义逻辑的独立逻辑,该逻辑确定某人是否有权访问某些端点.但是,无论我尝试什么,都不会执行.以下是我的OAuth2配置和我对该主题的调查结果.授权服务器:

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private AuthenticationManagerBuilder authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)throws Exception {
        endpoints.authenticationManager(authentication -> authenticationManager.getOrBuild().authenticate(authentication)).tokenStore(tokenStore);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("CLIENT_NAME")...;
    }

}
Run Code Online (Sandbox Code Playgroud)

资源服务器:

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }

    @Override
    public void configure(final ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(tokenStore); …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-boot spring-security-oauth2

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

查询存储在单独集合中的@Embeddable对象

我有一个简单的关系,其中实体具有许多特定于它的地址,定义为:

@Entity
public class Corporation {

    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(name = "addresses_table", joinColumns = @JoinColumn(name = "corporation_id"))
    private List<Address> addresses = new ArrayList<>();
}
Run Code Online (Sandbox Code Playgroud)

Address类都被注解@Embeddable。这非常有用,因为公司的每次更新都会删除其所有地址,然后插入新地址。这正是我要寻找的行为。我试过其他选项(OneToManyManyToMany)因为我需要赴汤蹈火,但仍没有得到简单的全部删除+插入所有的行为导致较差的性能。

但是,有一个简单的要求,即我需要能够按某些条件查询地址。基本上,这可以归结为一种简单的findAll(Pageable pageable, Specification spec)方法。这对于当前和将来的用例就足够了。

现在问题来了,可嵌入对象不是Entitys,因此我无法为其创建Spring数据存储库。我能想到的唯一选择是:

  1. 使用本机实体管理器实现自定义存储库,但是我不确定如何在代码方面以及它是否可以支持泛型方面做到最佳Specification。如果不能,我仍然可以忍受,因为将在其上搜索地址的字段不会改变。
  2. 进行一些联接查询select sth from Corporation c join c.addresses,然后根据地址属性限制结果。在这里我再次不确定这是否行得通,并且像直接对地址表进行简单排队一样表现出色

无论是关于所描述的选项还是用于其他替代方案,任何建议都将受到赞赏。

hibernate jpa spring-data spring-data-jpa

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

如何使用Spring Data正确处理JPA实体中的version字段

我有一个非常简单的域模型,一个实体具有一个version字段,以便使用JPA(api v2.2)提供的乐观锁定功能。我使用的实现是Hibernate v5.3.10.Final。

@Entity
@Data
public class Activity {

    @Id
    @SequenceGenerator(name = "gen", sequenceName = "gen_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
    private Long id;

    @Column
    private String state;

    @Version
    private int version;
}
Run Code Online (Sandbox Code Playgroud)

然后对该实体进行简单的操作,例如临时更改其阶段:

@Transactional
public Activity startProgress(Long id) {
    var entity = activityRepository.findById(id).orElseThrow(RuntimeException::new);

    if (entity.getState() == "this") { // or that, or else, etc
        // throw some exceptions in some cases
    }

    entity.setState("IN_PROGRESS");

    return activityRepository.saveAndFlush(entity);
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的结果是,有一种更新实体的方法,并且该更新还应该增加版本。如果存在不匹配,我希望会抛出a ObjectOptimisticLockingFailureExceptionOptimisticLockingException。我还希望version在对象中具有该字段的更新值,因为我要将其返回给客户端。

我尝试过的几种选择:

  • 只需调用 …

hibernate jpa spring-data spring-data-jpa

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