Avoid same file of File server been picked up from different services - java

I have a file server and two integration servers. Each integration server has the same task. It will pick up file from "incoming" folder of the file server, and then will process it. This service use Spring Integration. Two servers' services work at the same time, which would ensure that even one integration server fails, the other will keep working. Because these two are independent from each other, my question is how can I make sure that both services don't pick up the same file from file server Also master-slave approach is not what I am looking for.

Looks like you can use a file locker. Some thing like this
<file:inbound-channel-adapter id="filesIn"
directory="file:${input.directory}" prevent-duplicates="true">
<file:nio-locker/>
</file:inbound-channel-adapter>
Check this documentation

Related

How to access configurations specified in XML of a jetty server from code

I've a jetty Server, what I want to configure for HTTPS traffic, I could do this, just simply modified some XML, and ini files (since this IoC is the preferred way), however I would like to access to some of these things which were configured in the xml from other classes, this is in the xml:
<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="KeyStoreType">PKCS12</Set>
<Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/keystore"/></Set>
<Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"/></Set>
...
What I want is to call the sslContextFactory.getKeyStorePath() function somewhere in my codebase. But where is this sslContextFactory instance, where can I find it, how can I have a reference to it?
I am not entirely clear on what you are asking, but I am going to take a stab at it anyway. It sounds like you are familiar with running Jetty as a distribution and editing Jetty XML and ini files but now want to use some of the Jetty functionality as part of your embedded code?
For SSLContextFactory specifically, the class is included as part of the jetty-util jar file that comes with the distribution. You could also download that jar file by itself on Maven Central.
The official documentation for Jetty includes several examples of embedded Jetty functionality. This page may help you, specifically the example on Multiple Connectors, which uses SSL.
In fact it is not likely to be able to access the jetty standalone server's SSLContextFactory. Jetty by definition can host multiple applications at the same time, and acts like a container for those java web applications.
One option may be to use the embedded jetty server (a solution here), where you can start the jetty server within a java application (e.g. a jar package) and hold the references to your SSLContextFactory instance so that you can reload it whenever you like.
Another option may be using the maven jetty runner, again from another customized application, controlling the SslContextFactory yourself. This I cannot confirm at the moment, bu will update you as soon as I try it.

How to make Jetty reload its xml config files automatically?

Is it possible to extend jetty's hot deployment feature to reload, automatically, its xml config files?
I would like to make the client, through the webapp, choose to enable or disable the HTTPS service. However, for enabling/disabling, at least one xml file must be modified, which requires restarting the server. However, I would not like to restart the server when the client configure this, I would like to change it on the fly.
Is it possible? If it is, how should I configure it?
If you want to create/edit/change active Connectors at runtime, do that in code, not with the XML.
Get access to the Server object and then CRUD the Connectors to your desired end.
See:
addConnector(Connector)
removeConnector(Connector)
setConnectors(Connector[])
getConnectors()

Avoid project's directory - Java EE app

I am writing and small app using Java EE. I am using Apache Tomcat v 7 and Eclipse as IDE. When I Run the project (Run on server) I get :
http://127.0.0.1:8080/java-web/lis
(That's fine)
But I don't know If there is some way to rewrite the [java-web] dir just to get :
http://my-local-app.dev/list
I suppose there is some way like in Apache Server using confing files and enabling
the mod_rewrite.
I'll apreciate your help. Thanks
In short: All of the pieces you want to change are components of your deployment environment. Unless you have a specific need to override them, it's usually easiest during development to use the URLs that are a little less pretty.
If you do want to alter them, you need to familiarize yourself with what the various parts of an HTTP URL mean. What you have in your test environment is this:
http:// 127.0.0.1:8080/java-web/list
protocol host port path
You could insert an entry into your hosts file listing my-local-app.dev at 127.0.0.1, but that would not change the port or the path.
The port is determined when Tomcat starts up and is 8080 by default. The general port for HTTP is 80, but specific permission is required to bind to ports below 1024. On Linux, the authbind package makes this pretty easy; on Windows, the necessary steps will depend on your version and configuration (e.g., if you have a Group Policy).
In Tomcat, Web applications are prefixed with their names in the path; it looks like your (hypothetical?) application is named java-web.war. You can install an application as the "root application", but this requires a little bit more configuration and is generally skipped in development.
All of this can indeed also be done using something like mod_rewrite, but that seems like overkill to have slightly prettier URLs for your dev machine.
If you want your application to respond to the my-local-app.dev, you need to purchase the "my-local-app.dev" domain and get a Java web hotel running on it.
If your web application is named "java-web" and you do not want the URL to reflect that, you need to tell Tomcat that you want your application deployed at the ROOT location where the name of the web application is not present in the URL. This is typically done in the deployment stage but unfortunately there is no standard location to say this for WAR files so this is vendor dependent. For example does Glassfish use an extra XML file in your deployment.
I believe Tomcat supports this for ROOT.war files. If not, you probably needs to set the META-INF/context.xml file. See https://tomcat.apache.org/tomcat-7.0-doc/config/context.html for details on what to put in this file - especially the context path.

Where to save a configuration file uploaded by admin?

I am working on a web application, developed using spring mvc and server is tomcat. Now one of the requirement is that admin can upload a spring related service configuration file(which is different from the spring service configuration file residing in src code) with some changes because we want to make it configurable. Now server will be restarted to get the modified changes.
Now I am confused about one thing, where I should upload this file(file system?) so that when server is started then configured listener will pick up new configuration file.
Pls suggest solution considering it as enterprose aplication.
Find out the reasonable place to keep your system files, other than in webapps/.
(I had kept next to log folder.)
Use Spring's PropertyPlaceholderConfigurer to read file locations from properties file.
Use same locations and Create ApplicationContext object, in a factory method (You can pass any number of files).
Use getBean on applicationContext object to use beans.

Streaming Dynamic Files from Spring MVC

I've got a Spring Web MVC application (and also a BlazeDS application, though not as relevant) where files are dynamically generated based on certain client actions.
I'd like to just map a certain directory on the file system to Spring MVC (or the app server) url and let it serve the files in that directory (with streaming and standard last-modified header support). Ideally, the mapped directory would be configured via the spring config, since I already have support per-machine for setting that up.
So, how can I do this? The best I can find so far is to write a controller that reads the file manually and streams it byte-by-byte. However, that seems far less than ideal. Is support for something like this already baked into Spring MVC or the standard application server spec?
Thanks!
If your processing model supports it, why not cut the middleman of the filesystem out of the picture completely and just stream the files back through the response stream as they are generated? Take a look at the AbstractExcelView and AbstractPDFView classes of Spring MVC to see some examples of how this is done.
or the standard application server spec?
Yes, there is. As you didn't mention which one you're using, I'll give a Tomcat-targeted answer. All you basically need to do is to add a Context element for /path/to/your/resources in /conf/server.xml:
<Context docBase="/path/to/your/resources" path="/resources" />
This way they'll be accessible through http://example.com/resources/...
Ideal for this is using an lightweight proxying server in front of your appserver, like a nginx or lighthttpd. You can configure it for serving static content, without calling your app.
If directory and files so dynamic, you can prepare real path to file at your controller and give this filepath to the frontend server, using headers. For example for nginx it's a X-Accel-Redirect header. Read more about this (and follow links for other http servers) there

Categories

Resources