Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.activemq.api.core.ActiveMQException;
- import org.apache.activemq.api.core.SimpleString;
- import org.apache.activemq.api.core.TransportConfiguration;
- import org.apache.activemq.api.core.client.ActiveMQClient;
- import org.apache.activemq.api.core.client.ClientConsumer;
- import org.apache.activemq.api.core.client.ClientMessage;
- import org.apache.activemq.api.core.client.ClientProducer;
- import org.apache.activemq.api.core.client.ClientSession;
- import org.apache.activemq.api.core.client.ClientSessionFactory;
- import org.apache.activemq.api.core.client.MessageHandler;
- import org.apache.activemq.api.core.client.ServerLocator;
- import org.apache.activemq.core.remoting.impl.netty.NettyConnectorFactory;
- public class Client
- {
- private static final String queueName = "queue.exampleQueue";
- private static final String propName = "myprop";
- ClientSessionFactory sf = null;
- ClientSession session = null;
- ClientProducer producer = null;
- ClientMessage message = null;
- ClientConsumer consumer = null;
- String name;
- public Client(String name){
- this.name = name;
- }
- public void initializeComponents(){
- try
- {
- // Step 3. As we are not using a JNDI environment we instantiate the objects directly
- /**
- * this map with configuration values is not necessary (it configures the default values).
- * If you modify it to run the example in two different hosts, remember to also modify the
- * server's Acceptor at {@link EmbeddedServer}
- */
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("host", "localhost");
- map.put("port", 61616);
- // -------------------------------------------------------
- ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
- sf = serverLocator.createSessionFactory();
- // Step 4. Create a core queue
- ClientSession coreSession = sf.createSession(false, false, false);
- if (!coreSession.queueQuery(new SimpleString(queueName)).isExists())
- coreSession.createTemporaryQueue(queueName, queueName);
- coreSession.close();
- // Step 5. Create the session, and producer
- session = sf.createSession();
- producer = session.createProducer(queueName); //
- // Step 7. Create the message consumer and start the connection
- consumer = session.createConsumer(queueName);
- session.start();
- // Step 8. Receive the message.
- consumer.setMessageHandler(new MessageHandler(){
- public void onMessage(ClientMessage message)
- {
- System.out.println("client " + name + " received message " + message.getStringProperty(propName));
- }
- });
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void sendMessage(String messageText){
- // Step 6. Create and send a message
- message = session.createMessage(false);
- message.putStringProperty(propName, messageText);
- try {
- System.out.println("Producer is going to send a message");
- producer.send(message);
- } catch (ActiveMQException e) {
- e.printStackTrace();
- }
- }
- public void cleanUpConnection(){
- if (sf != null)
- {
- sf.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment