Biblioteca Java - Rev 31
Subversion Repositories:
(root)/Frameworks and Technologies/TestActiveMQ2/src/main/java/com/linkscreens/activemq/server/Server.java
package com.linkscreens.activemq.server;
import com.linkscreens.activemq.protocol.ProtocolType;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.ArrayList;
/**
* Created by evo2 on 7/29/2015.
*/
public class Server implements ExceptionListener {
private static int ackMode;
private static String messageBrokerUrl;
private Session session;
private boolean transacted = false;
private MessageProducer replyProducer;
private Connection connection;
private ArrayList<ServerMessageHandler> handlers = new ArrayList<ServerMessageHandler>();
static {
messageBrokerUrl = "tcp://localhost:61616";
ackMode = Session.AUTO_ACKNOWLEDGE;
}
public Server() throws JMSException {
System.out.println("Starting ActiveMQ server.");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
connection = connectionFactory.createConnection();
connection.start();
this.session = connection.createSession(this.transacted, ackMode);
}
public void addMessageHandler(ProtocolType protocol) {
try {
Destination adminQueue = this.session.createQueue(protocol.toString());
//Setup a message producer to respond to messages from clients, we will get the destination
//to send to from the JMSReplyTo header field from a Message
//reuse the same MessageProducer for sending messages to different destinations; just create it with a null destination and specify it on the send method.
this.replyProducer = this.session.createProducer(null);
this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//Set up a consumer to consume messages off of the admin queue
MessageConsumer consumer = this.session.createConsumer(adminQueue);
ServerMessageHandler handler = new ServerMessageHandler(protocol, session, replyProducer);
consumer.setMessageListener(handler);
handlers.add(handler);
System.out.println("Handler added for protocol "+protocol.toString());
} catch (JMSException e) {
//Handle the exception appropriately
e.printStackTrace();
}
}
public void shutdown(){
try {
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
* Handling JMS exceptions.
* @param e
*/
public synchronized void onException(JMSException e) {
System.out.println("JMS exception has occurred.");
if(e!=null)
e.printStackTrace();
}
public static void main(String[] args) throws JMSException {
Server server = new Server();
server.addMessageHandler(ProtocolType.ECHO);
server.addMessageHandler(ProtocolType.DUMMY);
}
}
import com.linkscreens.activemq.protocol.ProtocolType;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.ArrayList;
/**
* Created by evo2 on 7/29/2015.
*/
public class Server implements ExceptionListener {
private static int ackMode;
private static String messageBrokerUrl;
private Session session;
private boolean transacted = false;
private MessageProducer replyProducer;
private Connection connection;
private ArrayList<ServerMessageHandler> handlers = new ArrayList<ServerMessageHandler>();
static {
messageBrokerUrl = "tcp://localhost:61616";
ackMode = Session.AUTO_ACKNOWLEDGE;
}
public Server() throws JMSException {
System.out.println("Starting ActiveMQ server.");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
connection = connectionFactory.createConnection();
connection.start();
this.session = connection.createSession(this.transacted, ackMode);
}
public void addMessageHandler(ProtocolType protocol) {
try {
Destination adminQueue = this.session.createQueue(protocol.toString());
//Setup a message producer to respond to messages from clients, we will get the destination
//to send to from the JMSReplyTo header field from a Message
//reuse the same MessageProducer for sending messages to different destinations; just create it with a null destination and specify it on the send method.
this.replyProducer = this.session.createProducer(null);
this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//Set up a consumer to consume messages off of the admin queue
MessageConsumer consumer = this.session.createConsumer(adminQueue);
ServerMessageHandler handler = new ServerMessageHandler(protocol, session, replyProducer);
consumer.setMessageListener(handler);
handlers.add(handler);
System.out.println("Handler added for protocol "+protocol.toString());
} catch (JMSException e) {
//Handle the exception appropriately
e.printStackTrace();
}
}
public void shutdown(){
try {
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
* Handling JMS exceptions.
* @param e
*/
public synchronized void onException(JMSException e) {
System.out.println("JMS exception has occurred.");
if(e!=null)
e.printStackTrace();
}
public static void main(String[] args) throws JMSException {
Server server = new Server();
server.addMessageHandler(ProtocolType.ECHO);
server.addMessageHandler(ProtocolType.DUMMY);
}
}