问题描述:

I have a Spring JMS application. Infact there is no UI. Just Spring configuration (JMS listener) and the Spring configuration is loaded by web.xml .

so when i deploy in server, the listener starts working.

But I do not want the web part, because, there no UI, It is just a project which listen to a Queue and do its processing. So I think it should be JAR and it should run standalone(or when i deploy in server) How to create such project/ JAR when deployed in server it automatically starts running. I do not want run a main class every time I update the JAR.

网友答案:

I have used an executable jar to launch a JMS queue before. You just have to make sure you have access to all the jar dependencies for Spring and JMS, which is a lot. This can be done by setting the classpath to point at the dependency jars or create an Uberjar and pack all the dependency jars in the executable jar.

Here is an example class that will start up ActiveMQ from a Jar when you set it as a the main-class in the jar manifest. A jms.pid will be created with the process id for process. You must set the paths to your Spring contexts for JMS in the ConfigurableApplicationContext.

public class Server {

    public static void main(String[] args) throws Exception {

        // Define Spring contexts required for JMS to run
        List<String> contexts = Arrays.asList( "classpath:applicationContext.xml", "classpath:spring/jmsContext.xml" );
        ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(contexts);

        // Get activeMQ from JMS context
        BrokerService broker = applicationContext.getBean( "broker" );

        // Start up activeMQ
        broker.start();

        // Get pid for this process
        String sysId = ManagementFactory.getRuntimeMXBean().getName();
        String pid = sysId.substring(0, sysId.indexOf("@"));

        // Write PID file
        File file = new File("jms.pid");
        DataOutputStream outs = new DataOutputStream(new FileOutputStream(file, false));
        outs.write(pid.getBytes());
        outs.close();

    }

}

Example Spring configuration for getting access to the BrokerService

<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
    <property name="config" value="classpath:org/activemq/xbean/activemq.xml" />
    <property name="start" value="true" />
</bean>
网友答案:

I have typically seen JMS applications run as Windows Services or Unix daemons. These provide you features that you can configure like restarting your JMS app if the server reboots, etc.

There are some commercial Java EE containers like Weblogic that provide start-up classes that you can use to start your JMS application when a node in the cluster starts. This provides console control over the JMS application / server. It doesn't sound like that is an option in your case though.

相关阅读:
Top