We are injecting the mongotemplate using the spring config
<mongo:mongo-client id="mongoClient" connection-string="${mongodb.connect-string}">
<mongo:client-settings read-preference="PRIMARY_PREFERRED" retry-writes="true" />
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${mongodb.database}" mongo-client-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
#Inject
private MongoTemplate mongoTemplate;
Now we are creating a separate integration checkpoint to ensure the mongo db is available and show a green or red traffic light. Is it possible to get the connection URI from the instantiated mongotemplate or I should use ${mongodb.connect-string} to build the integration point?
In the older version of mongotemplate (springDataMongoDb : 1.8.2) we were able to use the following method to get the address
ServerAddress address = mongoTemplate.getDb().getMongo().getAddress();
but with spring 3.4.6 it is not possible to get the address using the above.
I would suggest using ${mongodb.connect-string} as you suggested since this is much more straight forward and doesn't require you to have to wire in the mongo template just to return a constant value when that is the exact propose of spring configs via the #Value annotation.
But a better solution to the green/red for mongo connectivity is using spring actuator which already has this functionality built in.
https://reflectoring.io/spring-boot-health-check/
Related
I would like to use connection pool for MongoDB. I have used MongoDB with Jongo and Spring MVC framework.
Here is my spring xml code for MongoDB configuration, which is working fine.
<!-- Mongo Configuration -->
<mongo:mongo id="mongo" host="${mongodb.host}" port="${mongodb.port}">
</mongo:mongo>
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="${mongodb.database}" username="${mongodb.username}" password="${mongodb.password}" />
I need to configure a connection pool. Can anyone guide, How to implement MongoDB connection pool with existing code?
Please check if the following helps
https://dzone.com/articles/multiple-mongodb-connectors-with-spring-boot
Also, a similar question seems to be asked
Connection pooling in Spring Boot and mongo db
I'm trying to use the Ignite Jdbc connection; with my goal to be able to call the cache from any client over Jdbc.
I've got a number of scenario's working; so have data loaded correctly; and can run sql queries 'directly' against the cache.
When I try to call from a separate client with
ResultSet rs = conn.createStatement().executeQuery("select * from my_table");
I hit an error:
class org.apache.ignite.IgniteCheckedException: Failed to find class with given class loader for unmarshalling (make sure same versions of all classes are available on all nodes or enable peer-class-loading) [clsLdr=URLClassLoader with NativeCopyLoader with RawResources(
Is there a way to prevent the Ignite jdbc connection from trying to do any unmarshalling?
I would like my client to be as agnostic as possible to the Ignite classes. For example; I would like to swap out calling mariaDb to Ignite - with as little code change as possible on the client side.
If I'm thinking about things the wrong way; then answer along the lines of No, that will never work because ... are more than welcome too.
Thanks
Brent
If you don't want copy libs to client's lib folder, you can turn on Peer Class Loading:
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
...
<!-- Explicitly enable peer class loading. -->
<property name="peerClassLoadingEnabled" value="true"/>
...
</bean>
Also, you can work with BinaryObject, instead of your classes. Here is some example of using sql with BinaryObjects. More information on the binary format is provided here.
I am new to Spring and going to develop a spring MVC Application.What is the best way to write root-context.xml for Bean class properties?
For database Connection I want to use Spring jdbc(JdbcTemplate).Can you please suggest me the best way to do so?
You're going to need to set up a DataSource and then create a JdbcTemplate bean that utilizes it. The Spring JDBC Reference Documentation provides examples and really good explanations on how to accomplish this.
Here's a basic bean definition for a datasource. The properties specified will depend on the database that you are using.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Then you can create a jdbcTemplate bean that uses the dataSource or you can instantiate the jdbcTemplate within your code.
The JdbcTemplate API mentions this:
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
I'd like to access share-config-custom.xml data from a Java bean in the Share webapp.
What is the equivalent to the following javascript syntax (which access the config root object) but in a Java context:
config.scoped['RepositoryLibrary']['root-node']
Is the share-config-custom translated to a bean itself? Or is there an API to read it from Java?
First, there is no "reasonable" way to use this API "looking" at one XML config file. In gen eral, the ConfigService creates a configuration merging from various sources. But looking directly at the XML should not be needed anyways.
That being said, the Javascript object config actually is a org.springframework.extensions.webscripts.ScriptConfigModel.
To get something equivalent in Java get yourself a reference to the ConfigService. To obtain the reference, let spring inject it in your custom bean:
<property name="configService" ref="web.config" />
Calling configService.getGlobalConfig() should get you the equivalent of config.scoped.
I know this is already answered, but Andreas' answer only got me halfway there. The configuration class is actually the XMLConfigService now.
Here's a code snippet that worked with Enterprise 4.1.* Alfresco:
Java class
import org.springframework.extensions.config.xml.XMLConfigService;
public class PDFValidate extends BaseJavaDelegate implements ExecutionListener
{
protected XMLConfigService configService;
public void setConfigService( XMLConfigService scriptConfigModel )
{
this.configService = scriptConfigModel;
}
Bean registration:
<bean id="AbstractWorkflowDelegate" parent="baseJavaDelegate" abstract="true" depends-on="activitiBeanRegistry" />
<bean id="PDFValidate" parent="AbstractWorkflowDelegate" class="com.epnet.alfresco.metadata.listener.PDFValidate">
<property name="repository" ref="repositoryHelper" />
<property name="configService" ref="web.config" />
</bean>
And from there, you can use the configService in your java code to get the config values.
The XMLConfigService is located in the spring-surf-core-configservice-1.2.0-SNAPSHOT.jar for my version of Alfresco.
I have a few cases where I want to get a connection to a configured database in a Spring Batch service class, for example in a processor or a custom writer. I can inject the data source into my class but I want to get a connection to it. The out of the box Spring Batch database readers and writers automagically get a connection so I want to use that rather than write my own connection code. So in the writer example say my class uses ItemWriterAdapter and in it I need to do a query. How can I grab an established connection to a configured data source?
Add this to your context.xml (assuming dataSource bean is defined)
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
In your Writer or Processor, inject this jdbcTemplate
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
Now you may query with Spring Jdbc using the same datasource http://static.springsource.org/spring/docs/current/spring-framework-reference/html/jdbc.html