Skip to content

Commit

Permalink
Use thread safe internal data structures, add a MyUseOfBackground com…
Browse files Browse the repository at this point in the history
…ponent

Once we have a component that depends on a Async component that all needs to resolve when creating the BeanScope.
  • Loading branch information
rbygrave committed Nov 19, 2024
1 parent 91fd6dd commit 5f79bb6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public BackgroundBean(@Nullable AtomicInteger intyAtomic) throws InterruptedExce
intyAtomic.incrementAndGet();
}

Thread.sleep(2000);
Thread.sleep(1200);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.example.myapp.async;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import io.avaje.inject.AsyncBean;
Expand All @@ -16,8 +15,7 @@ public class BackgroundBeanFactory {
@Bean
@Named("factory")
BackgroundBean lazyInt(@Nullable AtomicInteger intyAtomic) throws InterruptedException {

System.out.println("StartedInit" + Thread.currentThread().getName());
System.out.println("StartedInit BackgroundBean() " + Thread.currentThread().getName());
return new BackgroundBean(intyAtomic);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.myapp.async;

import io.avaje.inject.Component;
import jakarta.inject.Named;

// @AsyncBean
@Component
public class MyUseOfBackground {

private final BackgroundBean backgroundBean;

public MyUseOfBackground(@Named("single") BackgroundBean backgroundBean) {
this.backgroundBean = backgroundBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ void test() {

// the async beans shouldn't slowdown initialization
assertThat(Duration.between(start, Instant.now()).toMillis()).isLessThan(1000);
// the async beans shouldn't slowdown initialization
assertThat(Duration.between(start, Instant.now()).toMillis()).isLessThan(1000);

// prove it's not just lazy
var beforeGet = Instant.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void beanScope_all_superClasses() {
.findFirst().orElse(null);

assertThat(inhEntry.keys())
.containsExactly(name(InhOne.class), name(InhBase.class), name(InhBaseBase.class),
.containsExactlyInAnyOrder(name(InhOne.class), name(InhBase.class), name(InhBaseBase.class),
name(InhBaseIface2.class), name(InhBaseIface3.class), name(InhBaseIface.class));
}
}
Expand All @@ -100,7 +100,7 @@ void beanScope_all_interfaces() {
.findFirst().orElse(null);

assertThat(extendIfaces.keys())
.containsExactly(name(ConcreteExtend.class), name(IfaceExtend.class), name(IfaseBase.class));
.containsExactlyInAnyOrder(name(ConcreteExtend.class), name(IfaceExtend.class), name(IfaseBase.class));
}
}

Expand All @@ -115,7 +115,7 @@ void beanScope_all_includesGenericInterfaces() {
.findFirst().orElse(null);

assertThat(hazRepo.keys())
.containsExactly(name(HazRepo.class), name(HazRepo$DI.TYPE_RepositoryHazLong));
.containsExactlyInAnyOrder(name(HazRepo.class), name(HazRepo$DI.TYPE_RepositoryHazLong));
}
}

Expand All @@ -130,7 +130,7 @@ void beanScope_all_interfaceWithParameter() {
.findFirst().orElse(null);

assertThat(hazRepo.keys())
.containsExactly(name(MyParam.class), name(IfaceParam.class), name(IfaceParamParent.class));
.containsExactlyInAnyOrder(name(MyParam.class), name(IfaceParam.class), name(IfaceParamParent.class));
}
}

Expand Down
12 changes: 4 additions & 8 deletions inject/src/main/java/io/avaje/inject/spi/DBeanMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* Map of types (class types, interfaces and annotations) to a DContextEntry where the
* entry holds a list of bean instances for that type.
*/
final class DBeanMap {
private static final Optional<Object> EMPTY = Optional.empty();
private final Map<String, DContextEntry> beans = new LinkedHashMap<>();
private final Set<String> qualifiers = new HashSet<>();
private final Map<String, DContextEntry> beans = new ConcurrentHashMap<>();
private final Set<String> qualifiers = Collections.synchronizedSet(new HashSet<>());

private NextBean nextBean;
private Class<? extends AvajeModule> currentModule;
Expand Down
7 changes: 2 additions & 5 deletions inject/src/main/java/io/avaje/inject/spi/DContextEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import jakarta.inject.Provider;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Entry for a given key (bean class, interface class or annotation class).
Expand All @@ -14,7 +11,7 @@
*/
final class DContextEntry {

private final List<DContextEntryBean> entries = new ArrayList<>(5);
private final List<DContextEntryBean> entries = Collections.synchronizedList(new ArrayList<>(5));

@Override
public String toString() {
Expand Down

0 comments on commit 5f79bb6

Please sign in to comment.