יום שני, 27 במאי 2013

ActiveMQ topic sender

Starting ActiveMQ service
Capture19
Use the following maven pom for my simple client test

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>Client</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Client</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
<repositories> 
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss.org Maven repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>  
</repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   <dependency>
	<groupId>javax.jms</groupId>
	<artifactId>jms</artifactId>
	<version>1.1</version>
</dependency>
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.8.0</version>
</dependency>
  </dependencies>
</project>

The ActiveMQ monitor can be run  from the following url http://localhost:8161


The code for the demo

  1: package com.mycompany.client;
  2: 
  3: import javax.jms.*;
  4: import javax.naming.InitialContext;
  5: import javax.naming.NamingException;
  6: import java.util.Properties;
  7: import java.util.logging.Level;
  8: import java.util.logging.Logger;
  9:  
 10: public class MessageProducer {
 11:  private String topicName = "myTopic.Programming";
 12:  
 13:  private String initialContextFactory = "org.apache.activemq"
 14: +".jndi.ActiveMQInitialContextFactory";
 15:  private String connectionString = "tcp://localhost:61616";
 16:   
 17: 
 18:  public void publishWithTopicLookup() {
 19:      try {
 20:          Properties properties = new Properties();
 21:          TopicConnection topicConnection = null;
 22:          properties.put("java.naming.factory.initial", initialContextFactory);
 23:          properties.put("connectionfactory.QueueConnectionFactory",
 24:            connectionString);
 25:          properties.put("topic." + topicName, topicName);
 26:         
 27:          
 28:           // initialize
 29:           // the required connection factories
 30:           InitialContext ctx = new InitialContext(properties);
 31:           TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx
 32:                            .lookup("QueueConnectionFactory");
 33:           topicConnection = topicConnectionFactory.createTopicConnection();
 34:          
 35:           TopicSession topicSession = topicConnection.createTopicSession(
 36:              false, Session.AUTO_ACKNOWLEDGE);
 37:            Topic topic = (Topic) ctx.lookup(topicName);
 38: 
 39:            javax.jms.TopicPublisher topicPublisher = topicSession
 40:                        .createPublisher(topic);
 41:         
 42:            String msg = "This is a test message";
 43:            TextMessage textMessage = topicSession.createTextMessage(msg);
 44:         
 45:            topicPublisher.publish(textMessage);
 46:            System.out.println("Publishing message " +textMessage);
 47:            topicPublisher.close();
 48:            topicSession.close(); 
 49:            topicConnection.close();
 50:      } catch (NamingException ex) {
 51:          Logger.getLogger(MessageProducer.class.getName()).log(Level.SEVERE, null, ex);
 52:      }
 53:          catch (JMSException e) {
 54:    throw new RuntimeException("Error in initial context lookup", e);
 55:   }
 56:  }
 57: }

The connections:
http://localhost:8161/admin/connections.jsp


Note I set a break point before the connection closing in order to inspect the connection in the connections page


Capture21


Capture20


The sample use JNDI in order to generate the ActiveMQ objects .
The context factory that  is used to generate the objects is  ActiveMQInitialContextFactory.
It is set in the following command:
properties.put("java.naming.factory.initial", initialContextFactory);


The created topic in the topics page (http://localhost:8161/admin/topics.jsp)Capture22

אין תגובות:

הוסף רשומת תגובה