MongoDB replication with Spring - java

I configured Spring with MongoDB on my local machine, without replication, and everything works fine.
I also have a replica set that works fine.
Now I tried to add the replica set, but the reads/writes still go to my local machine!
This is my configuration, host1-3 are virtual machines:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:repositories base-package="my.repositories" />
<mongo:mongo replica-set="host1:27017,host2:27017,host3:27017" />
<mongo:db-factory dbname="my_db" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
Anyone has ideas/suggestions? Thanks!
Edit:
Ok so I found the problem, just needed to add mongo-ref to the factory. So the correct configuration is:
<mongo:mongo id="mongo" replica-set="host1:27017,host2:27017,host3:27017" />
<mongo:db-factory dbname="my_db" mongo-ref="mongo" />

Ok so I found the problem, just needed to add mongo-ref to the factory. So the correct configuration is:
<mongo:mongo id="mongo" replica-set="host1:27017,host2:27017,host3:27017">
<mongo:db-factory dbname="my_db" mongo-ref="mongo" />

Writes will always go to the primary. Reads go to the primary by default unless you change the Read Preference in your use of the Java driver.

Related

upgrading Apache Camel 2 to 3, where did Main.setApplicationContextUri() method go?

I have an old codebase that uses camel 2.something. We have to upgrade to camel 3.14 for Nexus-flagged security issues. I'm going through the Upgrade Camel 2 to 3 docs. I see that Main moved packages, which I did, but now I'm getting an error that Main.setApplicationContextUri("camel-context.xml") is no longer a method on Main. I can't find it, nor a replacement in the upgrade docs.
What am I missing?
Also, if there's a way to do a simple camel config via beans and annotations instead of xml, I'd love a pointer to that. I'm also running into an issue parsing the camel-context.xml, so if I can get rid of that whole mess, I'd love to.
We are using Java 8 so I think the latest that works with that is Camel 3.14.6 so that's what I'm trying.
Here's our camel-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
<!-- ================================== -->
<!-- Define camel context -->
<!-- ================================== -->
<camel:camelContext id="camel" trace="false">
<camel:propertyPlaceholder id="queriesConfig" location="lib/queries.properties" />
<!-- Location for route scan -->
<camel:package>com.blah.listener</camel:package>
</camel:camelContext>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="bean1" class="com.foo.OurThing" />
<bean id="bean2" class="com.bar.OtherThing" />
Edit: Just found this. Are we supposed to convert camel-context.xml to application.properties? We also have an app-context.xml for basic Spring configuration, and there's a reference to the camel-context.xml there. I wonder if we replace camel-context.xml with application.properties, what to do with this reference? Just remove it, or change it?

primary attribute not allowed inside bean tag

I have tried to set the default validation message file to messages.properties file. It worked. However, for some reason the primary attribute inside validator bean is marked red in the applicationContext.xml file.
The error message, "Attribute primary is not allowed here".
I have tried to find the reason of this in many resources and cross-checked many possibilities, but everything seems fine with my configuration.
I use Intellij Idea, Spring version - 3.2.17 RELEASE, Hibernate version - 3.6.10.Final.
Can anyone explain the reason ?
applicationContext.xml -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<context:component-scan base-package="com.test.leavemanagement"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
</bean>
<mvc:annotation-driven validator="validator"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" primary="true">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
</beans>

Having trouble setting up my pom.xml correctly to pass a variable to an XML file

So I'm fumbling my way through this, it's a project that I'm taking the reigns of because nobody else can (original dev left, nobody else with any experience to do it) but I'm trying my hardest here. My apologies if what I have already makes no sense, I'm not a developer really.
So I have my pom.xml file, and in the resources section I have it pulling values from some resource directories, depending on the profile used:
<resources>
<resource>
<directory>src/main/resources/${profile.name}</directory>
<includes>
<include>../../webapp/WEB-INF/applicationContext.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
One line in src/main/resources/test/application.properties is the following:
app.api.databaseUrl=jdbc:mysql://databasehostname:3306/testDatabase
You'll notice up above that I tried getting it to place this value in my applicationContext.xml file, so that a different database can be used for test and prod profiles - but that piece isn't working. It seems to be pulling all the values from application.properties, but I can't figure out how to put those in applicationContext.xml. Here's what I have in that file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${app.api.databaseUrl}"/>
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="cartDAO" class="com.crown.apps.cart.dao.impl.CartDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
When building the war file, "${app.api.databaseUrl}" does not change to the correct value, it stays the same.
I'm sure I'm doing something completely and utterly wrong, I'm sure my pom.xml setup is wrong but I don't know where to begin fixing it. Any advice?
Looks like you are using Spring 3.x, so please check this article. Basically, you have to specify the location of properties file in your beans configuration XML: <context:property-placeholder location="classpath:foo.properties" />

Error validating the file spring-ldap.xsd

i have this error in my bean configuration, but for another project works.. the xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
but in one project i have this error:
referenced file contains error http://www.springframework.org/schema/ldap/spring-ldap.xsd
UPDATE:
i modify my beam like this and work (i added conf. for pooling but is not important):
<!-- LDAP config -->
<bean id="contextSourceTarget" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${ldap.url}" />
<property name="base" value="${ldap.base}" />
<property name="userDn" value="${ldap.userDn}" />
<property name="password" value="${ldap.password}" />
<property name="pooled" value="false" />
</bean>
<bean id="contextSource"
class="org.springframework.ldap.pool.factory.PoolingContextSource">
<property name="contextSource" ref="contextSourceTarget" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
I guess it's because Pivotal's site is displaying captcha before showing the actual XSD,
see Spring schemaLocation fails when there is no internet connection
It depends on whether you get the error in the IDE or in the runtime.
If it's a specialized XML editor you should refer to the documentation of your editor on how to override/specify schema location, if it's in the runtime likely you have to check your project dependencies(pom.xml if it's a maven project).
Spring uses its own mechanism to get the XSD in classpath library (runtime).
Uses following files
META-INF/spring.handlers (to parse tag xml to object by an Handler)
example of spring-core-3.2.2.jar
http://www.springframework.org/schema/c=org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler
http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler
http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler
META-INF/spring.schemas (to identify the correct xsd used)
example of spring-core-3.2.2.jar
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd
http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd
http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd
http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd
The IDE want resolve the location of the XSD by schemaLocation, for this reason I suggest to remove XSD validation on your Eclipse.
Windows->Preferences->Validation-> "Suspend all validators"

Spring MVC - fetching wrong message bundle

I have a strange problem using Spring MVC message bundles: the wrong message bundle file is being fetched. I have double-checked, and in my Java controller class I have the fr_FR locale, but Spring tags (appContext.getMessage(code, null, locale); in the class as well) return me English messages!
What is going on?
I am developing portlets for Liferay Portal. Let me show you parts of my code:
in applicationContext.xml:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
in my JSPs I have code looking like this:
...
<spring:message code="button.help"/>
...
and the paths to my messages look like this:
development:
/src/main/webapp/WEB-INF/classes/messages.properties (English, default)
/src/main/webapp/WEB-INF/classes/messages_fr.properties
deployed in Tomcat
/webapps/MY_APP/WEB-INF/classes/messages.properties
/webapps/MY_APP/WEB-INF/classes/messages_fr.properties
Try setting the language (to fr_FR ) in your browser. In firefox version that I use, its at
Edit ->Preferences -> Content -> Langauges
And use the move-up or move-down buttons to have fr_FR as the top preference.
This makes the browser send requests with the prefered locale set.
I had a similar issue when my localized message file had a bad unicode character (the
\uXXX form).
Neither Java, Spring nor Tomcat did write an information about it, no feedback at all.
After fixing the file, everything suddenly started to work.
Regards
Frist you have to make sure that liferay supports the locale you want. In addition you have to add your supported locales in the portlet.xml for each portlet.
portlet.xml:
<portlet>
...
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
...
</portlet>
If you miss one of both it just won't work. Check if you added the supported-locale for this portlet as well.
You must have an interceptor in applicationContext like
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
You also need
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
On the first JSP page of my project I gave a choice for languages like that:
<span style="float: right"> en | fr </span>
Please tell me if this helped you.
P.S. My messages_*.properties files are in a source folder src/main/resources, not in webapp, I don't know if this matters.
P.P.S. Useful tutorial here:
http://springbyexample.org/examples/basic-webapp-internationalization.html
I would like to add also that at the beginning of your xml you should have stuff like:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
It is necessary in order to recognize the prefixes such as mvc. Make sure you have it.

Categories