001/*
002 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
003 * in compliance with the License. You may obtain a copy of the License at
004 *
005 * http://www.apache.org/licenses/LICENSE-2.0
006 *
007 * Unless required by applicable law or agreed to in writing, software distributed under the License
008 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
009 * or implied. See the License for the specific language governing permissions and limitations under
010 * the License.
011 */
012package org.atteo.moonshine.hornetq;
013
014import java.util.ArrayList;
015import java.util.HashSet;
016import java.util.List;
017import java.util.Set;
018
019import javax.xml.bind.annotation.XmlElement;
020import javax.xml.bind.annotation.XmlElementWrapper;
021import javax.xml.bind.annotation.XmlRootElement;
022
023import org.atteo.evo.config.XmlDefaultValue;
024import org.atteo.moonshine.TopLevelService;
025import org.hornetq.api.core.TransportConfiguration;
026import org.hornetq.core.config.Configuration;
027import org.hornetq.core.config.impl.ConfigurationImpl;
028import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
029import org.hornetq.core.server.JournalType;
030import org.hornetq.jms.server.config.ConnectionFactoryConfiguration;
031import org.hornetq.jms.server.config.JMSConfiguration;
032import org.hornetq.jms.server.config.JMSQueueConfiguration;
033import org.hornetq.jms.server.config.TopicConfiguration;
034import org.hornetq.jms.server.config.impl.JMSConfigurationImpl;
035import org.hornetq.jms.server.config.impl.JMSQueueConfigurationImpl;
036import org.hornetq.jms.server.embedded.EmbeddedJMS;
037
038import com.google.inject.Module;
039
040/**
041 * Starts HornetQ JMS server.
042 *
043 * <p>
044 * Currently the transport supports only clients in the same JVM.
045 * </p>
046 */
047@XmlRootElement(name = "hornetq-server")
048public class HornetQServer extends TopLevelService {
049    /**
050     * List of JMS queues to create.
051     */
052    @XmlElementWrapper(name = "queues")
053    @XmlElement(name = "queue")
054    List<String> queues;
055
056    @XmlElement
057    @XmlDefaultValue("${dataHome}/jms/journal")
058    private String journalDirectory;
059
060    @XmlElement
061    @XmlDefaultValue("${dataHome}/jms/bindings")
062    private String bindingsDirectory;
063
064    @XmlElement
065    @XmlDefaultValue("${dataHome}/jms/largeMessages")
066    private String largeMessagesDirectory;
067
068    @Override
069    public Module configure() {
070        return null;
071    }
072
073    private EmbeddedJMS jms;
074
075    @Override
076    public void start() {
077        jms = new EmbeddedJMS();
078
079        List<ConnectionFactoryConfiguration> connections = new ArrayList<>();
080        List<JMSQueueConfiguration> queueConfigs =  new ArrayList<>();
081        for (String queue : queues) {
082            queueConfigs.add(new JMSQueueConfigurationImpl(queue, "", true, ""));
083        }
084        List<TopicConfiguration> topics = new ArrayList<>();
085        JMSConfiguration jmsConfig = new JMSConfigurationImpl(connections, queueConfigs, topics, null);
086
087        jms.setJmsConfiguration(jmsConfig);
088
089        Configuration config = new ConfigurationImpl();
090        config.setSecurityEnabled(false);
091        config.setJournalType(JournalType.NIO);
092        config.setJMXManagementEnabled(true);
093        config.setPersistenceEnabled(true);
094        config.setJournalDirectory(journalDirectory);
095        config.setBindingsDirectory(bindingsDirectory);
096        config.setLargeMessagesDirectory(largeMessagesDirectory);
097
098        Set<TransportConfiguration> transports = new HashSet<>();
099        transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
100        config.setAcceptorConfigurations(transports);
101
102        jms.setConfiguration(config);
103        try {
104            jms.start();
105        } catch (Exception e) {
106            throw new RuntimeException("Cannot initialize JMS server", e);
107        }
108    }
109
110    @Override
111    public void close() {
112        try {
113            jms.stop();
114        } catch (Exception e) {
115            throw new RuntimeException("Cannot stop JMS server", e);
116        }
117    }
118}