How to set the mongo db path when using axon-mongo

561 views
Skip to first unread message

Edison Xu

unread,
Mar 14, 2017, 2:49:46 AM3/14/17
to Axon Framework Users
Hi,

I'm trying to use Mongo db as the repository for events, and through the doc, there's a MongoEventStorageEngine is provided in the module axon-mongo.
Here's my configure class
@Configuration
public class AxonConfiguration {

@Value("${mongodb.url}")
private String mongoUrl;

@Value("${mongodb.dbname}")
private String mongoDbName;

@Value("${mongodb.events.collection.name}")
private String eventsCollectionName;

@Value("${mongodb.events.snapshot.collection.name}")
private String snapshotCollectionName;

@Bean
Serializer axonJsonSerializer() {
return new JacksonSerializer();
}

@Bean
public EventStorageEngine eventStore(){
return new MongoEventStorageEngine(
axonJsonSerializer(),null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());
}

@Bean(name = "axonMongoTemplate")
public MongoTemplate axonMongoTemplate() {
MongoFactory mongoFactory = new MongoFactory();
mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoUrl)));
MongoClient mongoClient = mongoFactory.createMongo();
MongoTemplate template = new DefaultMongoTemplate(mongoClient, mongoDbName, eventsCollectionName, snapshotCollectionName);
return template;
}

}

It's actually working but in the console, I found there are two mongo connection, one is to the path I provided, and another is trying to connect localhost. 
It seems after introducing this module, Spring will automatically do some initializing work to connect to a mongo db at localhost.
How can I make it avoid connecting localhost? Or can I configure the path to connect for the auto initialization?

Thanks,

Edison Xu

unread,
Mar 14, 2017, 3:10:20 AM3/14/17
to Axon Framework Users
Solved.
It turns out that extracting the MongoClient initialization part into a method will solve this.
Clearly, Axon will check the every existence of a Bean returning MongoClient, and it will create a new one from the MongoFactory class with default config -> localhost.

But I didn't find the class doing this work in the axon-mongo module, even in the AxonAutoConfiguration of the axon-spring-boot-autoconfigure module.

在 2017年3月14日星期二 UTC+8下午2:49:46,Edison Xu写道:

Allard Buijze

unread,
Mar 14, 2017, 4:41:14 AM3/14/17
to Axon Framework Users
It's not Axon trying to connect to Mongo, but Spring Boot AutoConfiguration, which detects a Mongo library.

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edison Xu

unread,
Mar 15, 2017, 1:48:27 AM3/15/17
to Axon Framework Users
thanks for clearing.

在 2017年3月14日星期二 UTC+8下午4:41:14,Allard Buijze写道:
Reply all
Reply to author
Forward
0 new messages