Configuring TCP communications with Spring Integration Issues - spring

I seem to be having some trouble configuring my Spring MVC backend to receive and send TCP messages. Looking at the configuration a user suggests in this question - how to plug a TCP-IP client server in a spring MVC application - I tried to place this configuration into my root-context.xml. However, for all of the tags it displays a message such as:
Unable to locate Spring NamespaceHandler for element 'int-ip:tcp-outbound-gateway' of schema namespace 'http://www.springframework.org/schema/integration/ip'
int-ip:tcp-outbound-gateway and int:gateway both display cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'int:gateway' (replace int:gateway with int-ip:tcp-outbound-gateway).
Here is my root-context.xml file:
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
http://www.springframework.org/schema/integration/ http://www.springframework.org/schema/integration/spring-integration.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<int:gateway id="gw"
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
default-request-channel="input"/>
<int-ip:tcp-connection-factory id="client"
type="client"
host="localhost"
port="1234"
single-use="true"
so-timeout="10000"/>
<int:channel id="input" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
reply-channel="clientBytes2StringChannel"
connection-factory="client"
request-timeout="10000"
remote-timeout="10000"/>
<int:transformer id="clientBytes2String"
input-channel="clientBytes2StringChannel"
expression="new String(payload)"/>
</beans>
What am I doing incorrectly? Also, some general tips as to how I could use Spring to send and receive TCP communications would be appreciated :)

It appears you don't have the spring-integration-ip and spring-integration-core jars on your classpath. You need to bundle them into your war or otherwise make them available on the classpath according to your app server's requirements.

Related

Spring Upgrade: Spring Integration JMS java.lang.AbstractMethodError: org.springframework.integration.config.xml.AbstractRouterParser.parseRouter

I am getting the below error while deploying my application in Weblogic. The functionality was working fine with the older Spring version. After upgrading the spring from 2 to 4, we are getting this error :
java.lang.AbstractMethodError:
org.springframework.integration.config.xml.AbstractRouterParser.parseRouter(Lorg/w3c/dom/Element;Lorg/springframework/beans/factory/support/BeanDefinitionBuilder;Lorg/springframework/beans/factory/xml/ParserContext;)V
Below is the XML file content :
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-4.3.xsd
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms-4.3.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-4.3.xsd
http://www.springframework.org/schema/integration/xml
http://www.springframework.org/schema/integration/xml/spring-integration-xml-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<ctx:component-scan base-package="com.abc.xyz"/>
<beans:bean id="resultToDocumentTransformer" class="org.springframework.integration.xml.transformer.ResultToDocumentTransformer"/>
<beans:bean id="resultToStringTransformer" class="org.springframework.integration.xml.transformer.ResultToStringTransformer"/>
<int:channel id="PBWCMAuditInputChannel"/>
<int:channel id="PBWCMInputJMSChannel"/>
<jms:message-driven-channel-adapter id="PBWCMInputJMSAdapter"
destination="FMOB_IN" extract-payload="true"
connection-factory="connectionFactory"
channel="PBWCMAuditInputChannel"
error-channel="errorChannel"/>
<int:service-activator id="PBWCMMessageAuditor"
input-channel="PBWCMAuditInputChannel"
output-channel="PBWCMInputJMSChannel"
ref="mobileMessageAuditor"
method="auditRequest"/>
<si-xml:xpath-router id="wcmRequestRouter" input-channel="PBWCMInputJMSChannel">
<si-xml:mapping value="WCM" channel="WCMChannel"/>
</si-xml:xpath-router>
<si-xml:xpath-router id="WCMRequestRouter" input-channel="WCMChannel">
<si-xml:xpath-expression expression="/faml/request/scrseqnumber"/>
<si-xml:mapping value="01" channel="WCM01ValidateChannel"/>
</si-xml:xpath-router>
<int:channel id="WCM01ValidateChannel"/>
<si-xml:validating-filter id="WCM01Validator"
input-channel="WCM01ValidateChannel"
output-channel="WCM01InChannel"
schema-location="classpath:xsd/request/WCM_01_Request.xsd"
discard-channel="invalidMessageChannel"/>
<int:channel id="WCM01InChannel"/>
<int:channel id="WCM01OutChannel"/>
<si-xml:unmarshalling-transformer id="WCM01Unmarshaller" unmarshaller="WCM01ReqUnmarshaller"
input-channel="WCM01InChannel"
output-channel="WCM01OutChannel"/>
<int:channel id="WCM01ResponseChannel" />
<int:service-activator id="WCM01ServiceActivator"
input-channel="WCM01OutChannel"
output-channel="WCM01ResponseChannel"
ref="WCMRequestProcessor"
method="processWCM01Request"/>
<jms:outbound-channel-adapter id="WCM01ResponseOutAdapter" destination="responseQueue" channel="WCM01ResponseChannel"/>
<beans:bean id="WCM01ReqUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<beans:property name="contextPaths">
<beans:list>
<beans:value>com.abc.xyz.jms.jaxb.WCM_01_request</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</beans:beans>
The JARs available in the lib folder are
org.springframework.integration-1.0.3.RELEASE.jar
org.springframework.integration.jms-1.0.3.RELEASE-1.0.3.RELEASE.jar
org.springframework.integration.stream-1.0.3.RELEASE-1.0.3.RELEASE.jar
spring-beans-4.3.20.RELEASE.jar
spring-context-4.3.18.RELEASE.jar
spring-integration-jms-4.3.17.RELEASE.jar
spring-integration-stream-4.3.17.RELEASE.jar
spring-integration-xml-4.3.17.RELEASE.jar
If I remove this XML from my web.xml (contextConfigLocation), my EAR gets deployed successfully.
Help me figure out the issue in the XML.
All the Spring Integration dependencies must be in the same version. In your case 4.3.17.RELEASE. Same applies for Spring Framework dependencies.
Note: both those versions are EOL already . You need to think to upgrade to the latest one : https://spring.io/projects/spring-integration#learn.
Also, please, study what is dependency management and how you can avoid extra config relying on the transitive dependencies by the library you use.
Another useful tool these days is Spring Boot with its version management: https://spring.io/projects/spring-boot

deploying rest api war and starting with App Manager produces error FAIL - Application at context path

First time working with web apps and created a simple springboot rest api and it worked fine locally. Deployed the war to a tomcat server by copying the war file into the /webapps/ directory and restarted tomcat.
I checked if this was successful by opening the tomcat web application manager page and can see the app listed:
http://1xx.xxx.xxx.xxx:8080/manager/
When I click start to get it running the following error appears:
FAIL - Application at context path [/first_restapi] could not be started
I searched around and most answers suggest ensuring all pom file dependencies are included and to check the logs. I checked the logs on the tomcat server and the following error stuck out:
Exception sending context initialized event to listener instance of class
[org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/security]
Offending resource: ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
I did not include a servlet-context.xml file as the tutorial did not show that. But I added it anyway and still got the same error. Can anyone help with this issue?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<security:http auto-config="true" create-session="stateless">
<security:intercept-url pattern="/**" access="ROLE_USER"
requires-channel="http"/>
<security:http-basic />
</security:http>
</beans:beans>
You have a dependency problem. For each XML namespace that you (effectively) use in your configuration file, you need the appropriate NamespaceHandler. So in your case, to use the:
beans or util namespace you need spring-beans.jar,
context or task namespace you need spring-context.jar,
mvc namespace you need spring-webmvc.jar,
security namespace you need spring-security-config.jar.
You certainly don't have the last one dependency.

Jetty JNDI configuration vs Spring lookup

I have got a problem with JNDI configuration on Jetty server. I cannot by any means configure it in a way that afterwards Spring (3.0.5) can retrieve JNDI variables.
I have some credentials which I do not want to store in properties so that it will not exist in git repo. My web application is running on Jetty (most recent version 9.2.3), thus I came up with idea to store this credentials in Jetty web application context. Jetty provides such solution with jetty-env.xml. So I have created jetty-env.xml file in my WEB-INF/ like following:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="webappCtx" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="username" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
<Arg>server/username</Arg> <!-- name -->
<Arg type="java.lang.String">myUsername</Arg> <!-- value -->
</New>
<New id="password" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
<Arg>server/password</Arg> <!-- name -->
<Arg type="java.lang.String">qwerty</Arg> <!-- value -->
</New>
</Configure>
After that I have defined binding in the web.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyWebApp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web App</display-name>
<!-- INITIALIZE SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- JETTY-ENV JNDI -->
<resource-ref>
<res-ref-name>server/username</res-ref-name>
<res-type>java.lang.String</res-type>
</resource-ref>
<resource-ref>
<res-ref-name>server/password</res-ref-name>
<res-type>java.lang.String</res-type>
</resource-ref>
</web-app>
And configured my Spring context in this way:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<jee:jndi-lookup id="serverUsername" jndi-name="server/username" expected-type="java.lang.String" resource-ref="true" />
<jee:jndi-lookup id="serverPassword" jndi-name="server/password" expected-type="java.lang.String" resource-ref="true"/>
<bean name="abstractServerConnectionFactory" class="my.package.ServerConnectionFactory" abstract="true">
<constructor-arg value="${server.host}"/>
<constructor-arg value="${server.port}"/>
<constructor-arg ref="serverUsername"/>
<constructor-arg ref="serverPassword"/>
</bean>
</beans>
After that I am starting Jetty with --add-to-startd=jndi enabled and I am always getting javax.naming.NameNotFoundException when creating serverUsername and serverPassword. I have tried many modfication, but non have seem to work. I have tried:
Changing org.eclipse.jetty.plus.jndi.Resource to org.eclipse.jetty.plus.jndi.EnvEntry and then refering to it via resource-env-ref in web.xml
Setting resource scope to JVM instead of webapp so changing <Arg><Ref refid="webappCtx"/></Arg> to <Arg></Arg> as mentioned here
Adding automatic binding without using web.xml as mentioned here
<Call name="bindToENC">
<Arg>server/username</Arg> <!-- binds server/username to java:comp/env/server/username for this webapp -->
</Call>
And many other, but it just does not work. Please give me a working example how I can get it to work. Thank you!
Ok so I have managed to get this working. The problem was the configuration. I am using Jetty 9 so according to the official documentation (http://www.eclipse.org/jetty/documentation/current/jndi.html#jndi-quick-setup) the only thing that needs to be done prior to using JNDI in Jetty is adding jndi module to start.d by doing --add-to-startd=jndi. And well, that's not exactly true, because this will enable JNDI, BUT WILL NOT INCLUDE jetty-env.xml contents (Jetty do not even touch it). I've been reading about the container lifecycle and noticed that in order to use JNDI one need to include following classes in the web application context configuration classes set:
org.eclipse.jetty.plus.webapp.EnvConfiguration
org.eclipse.jetty.plus.webapp.PlusConfiguration
This is done by default in $JETTY_HOME/etc/jetty-plus.xml, which is config file for plus module. So in order to add this classes into the Jetty container lifecycle, and by this include and parse jetty-env.xml, one need to enable plus module in addition to jndi module (jndi does not depend upon plus)! Thus I have changed my start.d config by invoking --add-to-startd=jndi,plus (no space between modules) and everything started working like a charm.

Spring config custom namespace

I have a standalone spring application with an embedded Apache FTP server. The config looks like this -
<?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:afs="http://mina.apache.org/ftpserver/spring/v1"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd">
<context:property-placeholder location="classpath:config.properties" system-properties-mode="OVERRIDE"/>
<afs:server id="server" anon-enabled="false">
<afs:listeners>
<afs:nio-listener name="default" port="2222"
idle-timeout="60" />
</afs:listeners>
<!-- other AFS config -->
</afs:server>
</beans>
I would like to load the port property of nio-listener from a properties files, but
<afs:nio-listener name="default" port="${ftp.port}"
idle-timeout="60" />
doesn't work, since port is defined in the xsd as xs:int. I'd like to know if there is any workaround (using SpEL?) that will allow me to use the AFS namespace and load the port property from a file or from system properties.
You could try with PropertyOverrideConfigurer.
The problem is that you need to know the bean name that the <afs:server> tag define (may be 'server') and the property type that <afs:listeners> define (may be a managed list of bean definitions).
Look at STS bean explorer to find correct answers and try whith something like
<context:property-override location="classpath:config.properties" />
server.listeners[0].port=2222
Other option is disable schema validation setting validating to false before refresh in the xml application context.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"}, false);
context.setValidating(false);
context.refresh();
After exploring a few options, I've decided that the easiest way is to step outside the afs namespace for the just the listener configuration. Final config looks like this -
<bean id="listenerFactory" class="org.apache.ftpserver.listener.ListenerFactory">
<property name="port" value="${ftp.port}" />
<property name="dataConnectionConfiguration">
<bean factory-bean="dataConnectionConfigurationFactory"
factory-method="createDataConnectionConfiguration" />
</property>
</bean>
<bean id="dataConnectionConfigurationFactory" class="org.apache.ftpserver.DataConnectionConfigurationFactory" />
<bean id="nioListener" factory-bean="listenerFactory" factory-method="createListener" />
<afs:server id="server" anon-enabled="false">
<afs:listeners>
<afs:listener name="default">
<ref bean="nioListener"/>
</afs:listener>
</afs:listeners>
<!-- other AFS config -->
</<afs:server>

spring social xml config

i have already read the spring social document but the part of configuration is Java based, but my project's configuration is xml based. so please tell me how config spring social in spring xml config file. thank you and sorry for my poor english
Posting your code and issues will help us to provide you the best solution. Refer to the link below may be that is what you are looking for
http://harmonicdevelopment.tumblr.com/post/13613051804/adding-spring-social-to-a-spring-mvc-and-spring
Take a look at the example xml config
https://github.com/SpringSource/spring-social-samples/tree/master/spring-social-showcase-xml/src/main/webapp/WEB-INF/spring
You have to create a social config xml file and you have to import to your root-context.xml file. Also, you may think about configure your app with spring security. It's depends of your project architecture.
Sample spring social xml config file :
<?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:social="http://www.springframework.org/schema/social"
xmlns:facebook="http://www.springframework.org/schema/social/facebook" xmlns:bean="http://java.sun.com/jsf/core"
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-3.2.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd">
<!-- Ensures that configuration properties are read from a property file -->
<context:property-placeholder location="file:${sampleapp.appdir}/conf/appparam.txt"/>
<!--
Configures FB and Twitter support.
-->
<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" />
<!--
Configures the connection repository. This application uses JDBC
connection repository which saves connection details to database.
This repository uses the data source bean for obtaining database
connection.
-->
<social:jdbc-connection-repository data-source-ref="sampleappDS" connection-signup-ref="accountConnectionSignup"/>
<!--
This bean is custom account connection signup bean for your registeration logic.
-->
<bean id="accountConnectionSignup" class="com.sampleapp.social.AccountConnectionSignup"></bean>
<!--
This bean manages the connection flow between the account provider and
the example application.
-->
<bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor">
<constructor-arg index="0" ref="connectionFactoryLocator"/>
<constructor-arg index="1" ref="connectionRepository"/>
</bean>
Sample root-context.xml :
<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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- Scan for Spring beans declared via annotations. -->
<context:component-scan base-package="com.sampleapp"/>
<context:annotation-config/>
<context:property-placeholder location="file:${sampleapp.appdir}/conf/appparam.txt"/>
<cache:annotation-driven/>
<!-- Root Context: defines shared resources visible to all other web components -->
<import resource="security-config.xml"/>
<import resource="classpath*:spring/bean-context.xml"/>
<import resource="classpath*:spring/persistence-config.xml"/>
<import resource="social-config.xml"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>

Resources