/** * Constructor used for a basic template configuration. * * @param mongoClient must not be {@literal null}. * @param databaseName must not be {@literal null} or empty. */ public MongoTemplate(MongoClient mongoClient, String databaseName) { this(new SimpleMongoDbFactory(mongoClient, databaseName), (MongoConverter) null); }
@Bean @Primary public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); }
@Bean public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); } }
/** * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} * instance configured in {@link #mongoClient()}. * * @see #mongoClient() * @see #mongoTemplate() * @return */ @Bean public MongoDbFactory mongoDbFactory() { return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); }
public static MongoDbFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception{ MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test"); MongoTemplate template = new MongoTemplate(mongoDbFactory); template.dropCollection("messages"); template.dropCollection("data"); for (String additionalCollection : additionalCollectionToDrop) { template.dropCollection(additionalCollection); } return mongoDbFactory; } }
/** * Constructor used for a basic template configuration. * * @param mongoClient must not be {@literal null}. * @param databaseName must not be {@literal null} or empty. */ public MongoTemplate(MongoClient mongoClient, String databaseName) { this(new SimpleMongoDbFactory(mongoClient, databaseName), (MongoConverter) null); }
mongoClient = new MongoClient(seeds, credentials, options); MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, configuration.getDbName());
/** * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} * instance configured in {@link #mongoClient()}. * * @see #mongoClient() * @see #mongoTemplate() * @return */ @Bean public MongoDbFactory mongoDbFactory() { return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); }
@Bean public MongoDbFactory mongoDbFactory() { return new SimpleMongoDbFactory(new MongoClient(), "test"); }
@Override protected MongoDbMessageStore getMessageGroupStore() throws Exception { MongoDbMessageStore mongoDbMessageStore = new MongoDbMessageStore(new SimpleMongoDbFactory(new MongoClient(), "test")); mongoDbMessageStore.afterPropertiesSet(); return mongoDbMessageStore; }
@Override protected MessageStore getMessageStore() throws Exception { MongoDbMessageStore mongoDbMessageStore = new MongoDbMessageStore(new SimpleMongoDbFactory(new MongoClient(), "test")); mongoDbMessageStore.afterPropertiesSet(); return mongoDbMessageStore; }
protected MongoDbFactory prepareMongoFactory(String... additionalCollectionsToDrop) { MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test"); cleanupCollections(mongoDbFactory, additionalCollectionsToDrop); return mongoDbFactory; }
@Override protected ConfigurableMongoDbMessageStore getMessageGroupStore() throws Exception { MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test"); ConfigurableMongoDbMessageStore mongoDbMessageStore = new ConfigurableMongoDbMessageStore(mongoDbFactory); mongoDbMessageStore.setApplicationContext(this.testApplicationContext); mongoDbMessageStore.afterPropertiesSet(); return mongoDbMessageStore; }
@Override protected MessageStore getMessageStore() throws Exception { MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test"); ConfigurableMongoDbMessageStore mongoDbMessageStore = new ConfigurableMongoDbMessageStore(mongoDbFactory); mongoDbMessageStore.setApplicationContext(this.testApplicationContext); mongoDbMessageStore.afterPropertiesSet(); return mongoDbMessageStore; }
@Test @MongoDbAvailable public void testWithCustomConverter() throws Exception { this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test")); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ConfigurableMongoDbMessageStore-CustomConverter.xml", this.getClass()); context.refresh(); TestGateway gateway = context.getBean(TestGateway.class); String result = gateway.service("foo"); assertEquals("FOO", result); context.close(); }
@Test @MongoDbAvailable public void testNonExistingEmptyMessageGroup() throws Exception { this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test")); MessageGroupStore store = getMessageGroupStore(); store.addMessagesToGroup(1, new GenericMessage<Object>("foo")); MessageGroup messageGroup = store.getMessageGroup(1); assertNotNull(messageGroup); assertThat(messageGroup.getClass().getName(), containsString("PersistentMessageGroup")); assertEquals(1, messageGroup.size()); }
@Test @Ignore("The performance test. Enough slow. Also needs the release strategy changed to size() == 1000") @MongoDbAvailable public void messageGroupStoreLazyLoadPerformance() { cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test")); StopWatch watch = new StopWatch("Lazy-Load Performance"); int sequenceSize = 1000; performLazyLoadEagerTest(watch, sequenceSize, true); performLazyLoadEagerTest(watch, sequenceSize, false); // System. out .println(watch.prettyPrint()); // checkstyle }
@Test @MongoDbAvailable public void testCountMessagesInGroup() throws Exception { this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test")); MessageGroupStore store = this.getMessageGroupStore(); Message<?> messageA = new GenericMessage<String>("A"); Message<?> messageB = new GenericMessage<String>("B"); store.addMessagesToGroup(1, messageA, messageB); assertEquals(2, store.messageGroupSize(1)); }
@Test @MongoDbAvailable public void testAddThenRemoveWithStringPayload() throws Exception { cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test")); MessageStore store = getMessageStore(); Message<?> messageToStore = MessageBuilder.withPayload("Hello").build(); store.addMessage(messageToStore); Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId()); assertNotNull(retrievedMessage); store.removeMessage(retrievedMessage.getHeaders().getId()); retrievedMessage = store.getMessage(messageToStore.getHeaders().getId()); assertNull(retrievedMessage); }
@Test @MongoDbAvailable public void testLastReleasedSequenceNumber() 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.setLastReleasedSequenceNumberForGroup(messageGroup.getGroupId(), 5); messageGroup = store.getMessageGroup(1); assertEquals(5, messageGroup.getLastReleasedMessageSequenceNumber()); }