Skip to content

Not able to set custom converters to MongoDbMessageStore #2936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
debesay opened this issue May 20, 2019 · 4 comments · Fixed by #2944
Closed

Not able to set custom converters to MongoDbMessageStore #2936

debesay opened this issue May 20, 2019 · 4 comments · Fixed by #2944

Comments

@debesay
Copy link

debesay commented May 20, 2019

Affects Version(s): <Spring Integration version>
spring-integration-mongodb
5.1.2.RELEASE

As explained in https://stackoverflow.com/questions/56215529/configurablemongodbmessagestore-failed-to-instantiate-org-springframework-messa#
there is a need to be able to add custom converters to MongoDbMessageStore or successfully inject ConfigurableMongoDbMessageStore with a MongDbTemplate containing custom converters.
Please assist.

@garyrussell garyrussell added the status: waiting-for-triage The issue need to be evaluated and its future decided label May 20, 2019
@artembilan
Copy link
Member

Well, that is really why we have introduced a ConfigurableMongoDbMessageStore: the way when you can inject your own MongoTemplate with any possible external configuration.

We can't abuse internally a provided instance because that one can be used in any other place.
So, if you provide a MongoTemplate we don't mutate it internally. Here is a logic:

if (this.mongoTemplate == null) {
			if (this.mappingMongoConverter == null) {
				this.mappingMongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(this.mongoDbFactory),
						new MongoMappingContext());
				this.mappingMongoConverter.setApplicationContext(this.applicationContext);
				List<Object> customConverters = new ArrayList<>();
				customConverters.add(new MessageToBinaryConverter());
				customConverters.add(new BinaryToMessageConverter());
				this.mappingMongoConverter.setCustomConversions(new MongoCustomConversions(customConverters));
				this.mappingMongoConverter.afterPropertiesSet();
			}
			this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mappingMongoConverter);
		}

So, you should consider to add those MessageToBinaryConverter and BinaryToMessageConverter into your external MongoTemplate instance.

The MongoDbMessageStore is so tied to the messaging that its internal MongoTemplate, together with those internal converters, is fully not feasible for any other scenarios.

But as Gary said in the mentioned SO question, there is no reason to worry about a ConfigurableMongoDbMessageStore: just enough to provide a MongoDbFactory and the first one will take care about the proper configuration for an internal MongoTemplate!

@artembilan artembilan added the status: waiting-for-reporter Needs a feedback from the reporter label May 28, 2019
@garyrussell
Copy link
Contributor

@artembilan But can't we add a hook to allow him to add his custom converter to the template? From the SO comments..

I tried the MongoDbMessageStore first but it could not decode/encode a field property in my pojo of type java.time.ZonedDateTime. I have added a custom converter for this to the MongoTemplate, which I am able to use with my MongoRepository dao with no problem. But I have not found a way to use these custom converters with the MongoDbMessageStore, hence trying out the ConfigurableMongoDbMessageStore

Seems like addCustomConverters() should be possible. No?

@artembilan
Copy link
Member

Yes, that we can do: add setCustomConverters(Object... converters) into the MongoDbMessageStore, but I'm against similar approach for the ConfigurableMongoDbMessageStore, since we can't mutate injected MongoTemplate

@garyrussell
Copy link
Contributor

Agreed.

@artembilan artembilan added in: mongodb type: enhancement and removed status: waiting-for-reporter Needs a feedback from the reporter status: waiting-for-triage The issue need to be evaluated and its future decided labels May 29, 2019
@artembilan artembilan self-assigned this May 29, 2019
@artembilan artembilan added this to the 5.1.6 milestone May 29, 2019
artembilan added a commit to artembilan/spring-integration that referenced this issue May 29, 2019
Fixes spring-projects#2936

* Add `MongoDbMessageStore.setCustomConverters()` to allow to inject
any custom converters for `payload` as well as header values

**Cherry-pick to 5.1.x**
artembilan added a commit to artembilan/spring-integration that referenced this issue May 30, 2019
Fixes spring-projects#2936

* Add `MongoDbMessageStore.setCustomConverters()` to allow to inject
any custom converters for `payload` as well as header values

**Cherry-pick to 5.1.x**
garyrussell pushed a commit that referenced this issue May 30, 2019
Fixes #2936

* Add `MongoDbMessageStore.setCustomConverters()` to allow to inject
any custom converters for `payload` as well as header values

**Cherry-pick to 5.1.x**
garyrussell pushed a commit that referenced this issue May 30, 2019
Fixes #2936

* Add `MongoDbMessageStore.setCustomConverters()` to allow to inject
any custom converters for `payload` as well as header values

**Cherry-pick to 5.1.x**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants