Search in sources :

Example 36 with SimpleMongoDbFactory

use of org.springframework.data.mongodb.core.SimpleMongoDbFactory in project ocvn by devgateway.

the class MongoTemplateConfig method shadowMongoTemplate.

/**
 * Creates a shadow template configuration by adding "-shadow" as postfix of database name.
 * This is used to replicate the entire database structure in a shadow/temporary database location
 *
 * @return
 * @throws Exception
 */
@Bean(autowire = Autowire.BY_NAME, name = "shadowMongoTemplate")
public MongoTemplate shadowMongoTemplate() throws Exception {
    MongoTemplate template = new MongoTemplate(new SimpleMongoDbFactory(new MongoClientURI(properties.getUri() + SHADOW_POSTFIX)));
    ((MappingMongoConverter) template.getConverter()).setCustomConversions(customConversions);
    return template;
}
Also used : SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoClientURI(com.mongodb.MongoClientURI) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Bean(org.springframework.context.annotation.Bean)

Example 37 with SimpleMongoDbFactory

use of org.springframework.data.mongodb.core.SimpleMongoDbFactory in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testRemoveMessageGroup.

@Test
@MongoDbAvailable
public void testRemoveMessageGroup() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageStore messageStore = this.getMessageStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    Message<?> message = new GenericMessage<String>("Hello");
    UUID id = message.getHeaders().getId();
    messageGroup = store.addMessageToGroup(1, message);
    assertNotNull(messageGroup);
    assertEquals(1, messageGroup.size());
    message = messageStore.getMessage(id);
    assertNotNull(message);
    store.removeMessageGroup(1);
    MessageGroup messageGroupA = store.getMessageGroup(1);
    assertEquals(0, messageGroupA.size());
    assertFalse(messageGroupA.equals(messageGroup));
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) UUID(java.util.UUID) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 38 with SimpleMongoDbFactory

use of org.springframework.data.mongodb.core.SimpleMongoDbFactory in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupIterator.

@Test
@MongoDbAvailable
public void testMessageGroupIterator() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store1 = this.getMessageGroupStore();
    MessageGroupStore store2 = this.getMessageGroupStore();
    Message<?> message = new GenericMessage<String>("1");
    store2.addMessagesToGroup("1", message);
    store1.addMessagesToGroup("2", new GenericMessage<String>("2"));
    store2.addMessagesToGroup("3", new GenericMessage<String>("3"));
    MessageGroupStore store3 = this.getMessageGroupStore();
    Iterator<MessageGroup> iterator = store3.iterator();
    assertNotNull(iterator);
    int counter = 0;
    while (iterator.hasNext()) {
        iterator.next();
        counter++;
    }
    assertEquals(3, counter);
    store2.removeMessagesFromGroup("1", message);
    iterator = store3.iterator();
    counter = 0;
    while (iterator.hasNext()) {
        iterator.next();
        counter++;
    }
    assertEquals(2, counter);
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 39 with SimpleMongoDbFactory

use of org.springframework.data.mongodb.core.SimpleMongoDbFactory in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testWithAggregatorWithShutdown.

// @Test
// @MongoDbAvailable
// public void testConcurrentModifications() throws Exception{
// MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
// final MongoDbMessageStore store1 = new MongoDbMessageStore(mongoDbFactory);
// final MongoDbMessageStore store2 = new MongoDbMessageStore(mongoDbFactory);
// 
// final Message<?> message = new GenericMessage<String>("1");
// 
// ExecutorService executor = null;
// 
// final List<Object> failures = new ArrayList<Object>();
// 
// for (int i = 0; i < 100; i++) {
// executor = Executors.newCachedThreadPool();
// 
// executor.execute(new Runnable() {
// public void run() {
// MessageGroup group = store1.addMessageToGroup(1, message);
// if (group.getUnmarked().size() != 1){
// failures.add("ADD");
// throw new AssertionFailedError("Failed on ADD");
// }
// }
// });
// executor.execute(new Runnable() {
// public void run() {
// MessageGroup group = store2.removeMessageFromGroup(1, message);
// if (group.getUnmarked().size() != 0){
// failures.add("REMOVE");
// throw new AssertionFailedError("Failed on Remove");
// }
// }
// });
// 
// executor.shutdown();
// executor.awaitTermination(10, TimeUnit.SECONDS);
// store2.removeMessageFromGroup(1, message); // ensures that if ADD thread executed after REMOVE, the store is empty for the next cycle
// }
// assertTrue(failures.size() == 0);
// }
protected void testWithAggregatorWithShutdown(String config) throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, this.getClass());
    context.refresh();
    MessageChannel input = context.getBean("inputChannel", MessageChannel.class);
    QueueChannel output = context.getBean("outputChannel", QueueChannel.class);
    Message<?> m1 = MessageBuilder.withPayload("1").setSequenceNumber(1).setSequenceSize(10).setCorrelationId(1).build();
    Message<?> m2 = MessageBuilder.withPayload("2").setSequenceNumber(2).setSequenceSize(10).setCorrelationId(1).build();
    input.send(m1);
    assertNull(output.receive(1000));
    input.send(m2);
    assertNull(output.receive(1000));
    for (int i = 3; i < 10; i++) {
        input.send(MessageBuilder.withPayload("" + i).setSequenceNumber(i).setSequenceSize(10).setCorrelationId(1).build());
    }
    context.close();
    context = new ClassPathXmlApplicationContext(config, this.getClass());
    input = context.getBean("inputChannel", MessageChannel.class);
    output = context.getBean("outputChannel", QueueChannel.class);
    Message<?> m10 = MessageBuilder.withPayload("10").setSequenceNumber(10).setSequenceSize(10).setCorrelationId(1).build();
    input.send(m10);
    assertNotNull(output.receive(2000));
    context.close();
}
Also used : MongoClient(com.mongodb.MongoClient) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext)

Example 40 with SimpleMongoDbFactory

use of org.springframework.data.mongodb.core.SimpleMongoDbFactory in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testCompleteMessageGroup.

@Test
@MongoDbAvailable
public void testCompleteMessageGroup() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    assertNotNull(messageGroup);
    Message<?> message = new GenericMessage<String>("Hello");
    store.addMessagesToGroup(messageGroup.getGroupId(), message);
    store.completeGroup(messageGroup.getGroupId());
    messageGroup = store.getMessageGroup(1);
    assertTrue(messageGroup.isComplete());
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Aggregations

SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)51 MongoClient (com.mongodb.MongoClient)38 Test (org.junit.Test)28 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)28 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)16 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)16 GenericMessage (org.springframework.messaging.support.GenericMessage)14 MessageGroup (org.springframework.integration.store.MessageGroup)12 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)10 Bean (org.springframework.context.annotation.Bean)9 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)8 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)8 MessageStore (org.springframework.integration.store.MessageStore)8 MongoClientURI (com.mongodb.MongoClientURI)6 Net (de.flapdoodle.embed.mongo.config.Net)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 ClaimCheckInTransformer (org.springframework.integration.transformer.ClaimCheckInTransformer)4 ClaimCheckOutTransformer (org.springframework.integration.transformer.ClaimCheckOutTransformer)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3