I need help to load/inject tomcat/conf/catalina/localhost/myapp.xml in my SpringJunit Test. I am testing my mail service from JUnit.
Mail Spring XML:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="#{contextParameters.mail_host}"/>
<property name="username" value="#{contextParameters.mail_username}" />
<property name="password" value="#{contextParameters.mail_password}" />
<property name="port" value="#{contextParameters.mail_port}" />
....
Tomcat MyApp.xml:
<Context reloadable="true">
<Parameter name="mail_host" value="XXXXX" override="true"/>
<Parameter name="mail_username" value="XXXXXX" override="true"/>
....
I need to inject my mail details from tomcat myapp.xml in spring-mail.xml. I can able to load my spring context properties and beans but not tomcat parameters. Help appreciated!.
Related
I have been trying for couple days to set up multiple ActiveMQ connections in Spring Integration using XML configuration.
I am using spring boot, SI looks for a bean called jmsConnectionFactory in the context and uses it. But what if I have to send/listen to jms messages from/to different ActiveMQ servers?
What I have right now is this:
<bean id="jmsConnectionFactory1"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
</property>
<property name="sessionCacheSize" value="10" />
<property name="cacheConsumers" value="false" />
</bean>
<bean id="jmsConnectionFactory2"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.1.59:61616" />
</bean>
</property>
<property name="sessionCacheSize" value="10" />
<property name="cacheConsumers" value="false" />
</bean>
...
<jms:message-driven-channel-adapter channel="jmsInChannel" destination-name="queue.demo" />
<int:channel id="jmsInChannel" />
...
When trying to start the spring boot app I get this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'jmsConnectionFactory' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'jmsConnectionFactory' in 'ActiveMQXAConnectionFactoryConfiguration' not loaded because #ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'
Action:
Consider revisiting the entries above or defining a bean named 'jmsConnectionFactory' in your configuration.
I came across this solution, https://stackoverflow.com/a/43401330/3367392 it's a java configuration. Also I looked into this but it's using camel https://stackoverflow.com/a/13288312/3367392
Is there a way to achieve the same for Spring Integration using XML configuration?
Ok so I got it, for a simple case I just had to add the right connectionFactory to the adapters like this
<jms:message-driven-channel-adapter channel="jmsInChannel"
destination-name="queue.demo"
connection-factory="jmsConnectionFactory1" />
I am working behind a firewall,where i am trying to access a soap web service.
i went over through the internet,but couldn't find anything for setting proxy in .
I am calling the webservice using spring-integration.
Spring-integration.xml
<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="connectionTimeout" value="10000"/>
<property name="readTimeout" value="10000"/>
</bean>
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
</bean>
<int:gateway id="requestGateway" service-interface="main.java.com.as.poller.RequestGateway"
default-request-channel="requestchannel" default-reply-timeout="20000">
<int:method name="pushNotification" />
</int:gateway>
<int-ws:outbound-gateway id="pointbalance"
uri="url"
marshaller="marshaller" unmarshaller="marshaller"
request-channel="pointbalancechannel" message-sender="messageSender"
message-factory="soapMessageFactory">
<int-ws:request-handler-advice-chain>
<int:retry-advice max-attempts="${retry_limit}">
</int:retry-advice>
<bean class="main.java.com.as.poller.RetryAdvice" />
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
I could find only examples using CommonsHttpMessageSender.But this is deprecated.Can anyone please help me on setting the proxy for HttpComponentsMessageSender for my xml configuration...
I did it using org.springframework.ws.transport.http.CommonsHttpMessageSender from a superb article https://onebyteatatime.wordpress.com/2009/04/08/spring-web-service-call-using-proxy-per-connection/ . But still i dont see any thing regarding HttpComponentsMessageSender.
I have an MBean (JMX) which is exposed through RMI in a JBoss AS 7.1 Server but I can't access it. I already follow all of the tutorials revolving around but it just cant work.
This is how I exposed my MBean
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>
<bean id="mBeanExporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry
key="test:name=foo"
value-ref="foo" />
</map>
</property>
<property name="server" ref="mbeanServer" />
</bean>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1399" />
</bean>
<bean id="serverConnector"
class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName" value="connector:name=rmi" />
<property name="serviceUrl"
value="service:jmx:rmi://192.168.1.108/jndi/rmi://192.168.1.108:1399/myconnector" />
<property name="server">
<ref local="mbeanServer" />
</property>
</bean>
How can I remotely access this in Jconsole ?
I've already tried these:
service:jmx:remoting-jmx://192.168.1.108:9999
service:jmx:rmi:///jndi/rmi://192.168.1.108:1090/jmxrmi
service:jmx:rmi:///jndi/rmi://192.168.1.108:1090/myconnector
And many more but none of those work.
What am I doing wrong or what should I do ?
On JBoss 7 /EAP6 is can't use rmi for remote jmx calls, JBoss uses remoting-jmx protocol for jmx.
You can see a full example in: Using Spring to call jmx bean on JBoss7 / EAP 6
I'm more than inexperienced with Apache Tomcat, so forgive me if it's a trivial question I'm asking.
My task is to change a rather big program so that it uses the connection from Tomcat instead of its own bean, so that you don't have to rebuild the code when the database changes.
This is the original bean definition (slightly changed - passwords and such...):
<bean id="ProjectDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
<property name="url"
value="jdbc:as400://127.0.0.1/TEST2;prompt=false;naming=sql;errors=full;date format=usa;date separator=/;time format=hms;time separator=:;transaction isolation=read committed;"/>
<property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver"/>
<property name="username" value="asdf"/>
<property name="password" value="asdf"/>
<property name="initialSize" value="${ProjectDS.initialSize}"/>
<property name="maxActive" value="${ProjectDS.maxActive}"/>
<property name="maxIdle" value="${ProjectDS.maxIdle}"/>
<property name="minIdle" value="${ProjectDS.minIdle}"/>
<property name="testOnBorrow" value="${ProjectDS.testOnBorrow}"/>
<property name="removeAbandoned" value="${ProjectDS.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${ProjectDS.removeAbandonedTimeout}"/>
<property name="logAbandoned" value="${ProjectDS.logAbandoned}"/>
</bean>
After reading several tutorials about Tomcat configuration, I did the following:
The new bean:
<bean id="ProjectDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>ProjectDS</value>
</property>
<property name="resourceRef" value="true"></property>
</bean>
server.xml :
<Resource name="ProjectDS" global="ProjectDS" auth="Container"
type="org.apache.commons.dbcp.BasicDataSource"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
url="jdbc:as400://127.0.0.1/TEST2"
username="asdf"
password="asdf" />
context.xml:
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink global="ProjectDS" name="ProjectDS" type="org.apache.commons.dbcp.BasicDataSource"/>
What I get is:
Exception processing Global JNDI Resources
javax.naming.NamingException: Cannot create resource instance
I read somewhere that I shouldn't put the above into the server.xml but into the web.xml, but I don't know where exactly. Could that be the problem?
First, rollback your context.xml and server.xml in your tomcat and/or. Here is a working configuration:
Create a file in src/main/webapp/META-INF named context.xml which contains:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="ProjectDS" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
url="jdbc:as400://127.0.0.1/TEST2"
username="asdf"
password="asdf" />
</Context>
Then, edit your bean in this way:
<bean id="ProjectDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/ProjectDS" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
Difference here is the java:comp/env/ProjectDS and the proxy unterface.
Hope it will work for you!
This was my first attempt at Spring with JNDI but getting the below mentioned exception when trying to create the ApplicationContext like:
ApplicationContext context = new ClassPathXmlApplicationContext("master-job.xml");
The Spring configuration file is as follows:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="/jdbc/Eqpstatus"/>
<property name="resourceRef" value="true" />
</bean>
<bean id="masterDao" class="com.dao.MasterDao">
<property name="dataSource" ref="dataSource"/>
</bean>
On Server i have the required resource entry for the JNDI name.
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver"
maxActive="10" maxIdle="2" maxWait="10000" name="jdbc/Eqpstatus"
password="xxxx" type="javax.sql.DataSource"
url="jdbc:oracle:thin:#(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xyz)))"
username="xxx"/>
The error i see is:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
Would highly appreciate any inputs on this as am new to Spring-JNDI integration.
First, I think you should use the dedicated tag instead of declaring a "JndiObjectFactoryBean" bean :
<!-- JNDI DataSource for J2EE environments -->
<jee:jndi-lookup id="dataSource" jndi-name="java:jdbc/rppsDS-PUB-PROTO" default-ref="localDataSource" />
<!-- local dataSource for JUnit integration tests -->
<bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
Then, you need a jndi.properties file (which could be directly in application server dir such as JBoss) with a content similar to :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
If you are using Tomcat and everything is fine with your Tomcat configuration; this should be enough:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/dataSource" />
Where jdbc/dataSource is the name defined in your Tomcat config.
EDIT:
My bad, forgot about the jndi.properties; there are two possibilities:
either provide a jndi.properties on your classpath or
set the values in a in the <jee:jndi-environment/> element of <jee:jndi-lookup /> (see reference)
The java.naming.factory.initial property needs to be set for sure, to something like org.apache.naming.java.javaURLContextFactory, possibly some other values as well, like:
java.naming.factory.url.pkgs=org.apache.naming
java.naming.factory.url.pkgs.prefixes=org.apache.naming
java.naming.provider.url=org.apache.naming
Also see reference.