use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.
the class FileConfigurationTest method testFileConfigurationOverride.
@Test
public void testFileConfigurationOverride() throws IOException, ActivationException, AssemblyException {
File testFile = File.createTempFile(FileConfigurationTest.class.getName(), "" + System.currentTimeMillis());
final File confDir = testFile;
final File dataDir = testFile;
final File tempDir = testFile;
final File cacheDir = testFile;
final File logDir = testFile;
SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
// START SNIPPET: override
FileConfigurationOverride override = new FileConfigurationOverride().withConfiguration(confDir).withData(dataDir).withTemporary(tempDir).withCache(cacheDir).withLog(logDir);
new FileConfigurationAssembler().withOverride(override).assemble(module);
// END SNIPPET: override
}
};
FileConfiguration config = assembler.module().findService(FileConfiguration.class).get();
assertEquals(testFile.getAbsolutePath(), config.configurationDirectory().getAbsolutePath());
}
use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.
the class CircuitBreakerManagementSample method main.
public static void main(String[] args) throws ActivationException, AssemblyException {
SingletonAssembler assembler = new SingletonAssembler() {
@Override
public // START SNIPPET: jmx
void assemble(ModuleAssembly module) throws AssemblyException {
// END SNIPPET: jmx
CircuitBreaker cb = new CircuitBreaker(3, 250, CircuitBreakers.in(IllegalArgumentException.class));
module.importedServices(TestService.class).setMetaInfo(new TestService(cb));
// START SNIPPET: jmx
// JMX Library
module.importedServices(MBeanServer.class).importedBy(MBeanServerImporter.class);
// CircuitBreakers in JMX
module.services(CircuitBreakerManagement.class).instantiateOnStartup();
}
};
TestService service = assembler.module().<TestService>findService(TestService.class).get();
// Seconds
int interval = 1;
System.out.println("CircuitBreaker JMX Support sample is now started.");
System.out.println();
System.out.println("A Service that randomly output some text or fail is called through a CircuitBreaker every " + interval + " seconds.");
System.out.println("In a few interval the CircuitBreaker will be turned off.");
System.out.println("Connect with a MBean browser (eg. VisualVM + MBean plugin) to use the turnOn operation on the CircuitBreakers.");
System.out.println();
System.out.println("Hit Ctrl-C to stop.");
System.out.println();
while (true) {
try {
Thread.sleep(interval * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.helloWorld();
}
}
use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.
the class EventsTest method testData.
@Before
public void testData() throws ActivationException, AssemblyException {
SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
module.values(UnitOfWorkDomainEventsValue.class, DomainEventValue.class);
}
};
list = new ArrayList<UnitOfWorkDomainEventsValue>();
{
ValueBuilder<UnitOfWorkDomainEventsValue> builder = assembler.module().newValueBuilder(UnitOfWorkDomainEventsValue.class);
builder.prototype().events().get().add(newDomainEvent(assembler, "Test1"));
builder.prototype().events().get().add(newDomainEvent(assembler, "Test2"));
builder.prototype().events().get().add(newDomainEvent(assembler, "Test3"));
builder.prototype().version().set("1.0");
builder.prototype().timestamp().set(System.currentTimeMillis());
builder.prototype().usecase().set("Test");
list.add(builder.newInstance());
}
{
ValueBuilder<UnitOfWorkDomainEventsValue> builder = assembler.module().newValueBuilder(UnitOfWorkDomainEventsValue.class);
builder.prototype().events().get().add(newDomainEvent(assembler, "Test4"));
builder.prototype().events().get().add(newDomainEvent(assembler, "Test5"));
builder.prototype().events().get().add(newDomainEvent(assembler, "Test6"));
builder.prototype().version().set("1.0");
builder.prototype().timestamp().set(System.currentTimeMillis());
builder.prototype().usecase().set("Test2");
list.add(builder.newInstance());
}
}
use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.
the class JMXTest method main.
public static void main(String[] args) throws InterruptedException, ActivationException, AssemblyException {
/*
Logger logger = Logger.getLogger( "" );
logger.setLevel( Level.FINE );
Logger.getLogger("sun.rmi").setLevel( Level.WARNING );
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel( Level.FINE );
logger.addHandler( consoleHandler );
*/
SingletonAssembler assembler = new SingletonAssembler() {
// START SNIPPET: assembly
public void assemble(ModuleAssembly module) throws AssemblyException {
// END SNIPPET: assembly
new EntityTestAssembler().assemble(module);
module.services(TestService.class, TestService2.class, TestService3.class).instantiateOnStartup();
module.entities(TestConfiguration.class);
module.values(TestValue.class);
module.objects(TestObject.class);
// START SNIPPET: assembly
new JMXAssembler().assemble(module);
module.services(JMXConnectorService.class).instantiateOnStartup();
module.entities(JMXConnectorConfiguration.class);
module.forMixin(JMXConnectorConfiguration.class).declareDefaults().port().set(1099);
}
};
// This allows user to connect using VisualVM/JConsole
while (true) {
Thread.sleep(10000);
}
}
use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.
the class LiquibaseServiceTest method testLiquibase.
@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
final SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
ModuleAssembly configModule = module;
// Create in-memory store for configurations
new EntityTestAssembler().assemble(configModule);
new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").withConfig(configModule, Visibility.layer).assemble(module);
new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
module.values(SomeValue.class);
// Set up Liquibase service that will create the tables
// START SNIPPET: assembly
new LiquibaseAssembler().withConfig(configModule, Visibility.layer).assemble(module);
// END SNIPPET: assembly
module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
}
@Override
public void beforeActivation(Application application) {
application.registerActivationEventListener(new ActivationEventListener() {
@Override
public void onEvent(ActivationEvent event) {
System.out.println(event);
}
});
}
};
Module module = assembler.module();
// START SNIPPET: io
// Look up the DataSource
DataSource ds = module.findService(DataSource.class).get();
// Instanciate Databases helper
Databases database = new Databases(ds);
// Assert that insertion works
assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
// END SNIPPET: io
database.query("select * from test", new Databases.ResultSetVisitor() {
@Override
public boolean visit(ResultSet visited) throws SQLException {
assertThat(visited.getString("id"), equalTo("someid"));
assertThat(visited.getString("foo"), equalTo("bar"));
return true;
}
});
Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {
@Override
public SomeValue map(ResultSet resultSet) {
ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
try {
builder.prototype().id().set(resultSet.getString("id"));
builder.prototype().foo().set(resultSet.getString("foo"));
} catch (SQLException e) {
throw new IllegalArgumentException("Could not convert to SomeValue", e);
}
return builder.newInstance();
}
};
// START SNIPPET: io
// Select rows and load them in a List
List<SomeValue> rows = new ArrayList<SomeValue>();
database.query("select * from test").transferTo(map(toValue, collection(rows)));
// Transfer all rows to System.out
Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
Aggregations