zeromq / jeromq-jms

JeroMQ JMS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connect jeromq-jms subscriber to zeroMQ Publisher

mueller-jens opened this issue · comments

I am trying to connect a jms based subscriber to a plain ZeroMQ Publisher. But the subscriber does not recive any message. Is there any way to achive this?

Publisher:

    Context context = ZMQ.context(1);

    final Socket publisherSocket = context.socket(ZMQ.PUB);

    publisherSocket.bind("tcp://*:9714");
    Thread t = new Thread(() -> {
        while (true) {

            // Write two messages, each with an envelope and content
            publisherSocket.sendMore("evt");
            publisherSocket.send("TestEvent");

        }

JMS Template configuration:

  final JmsTemplate template = new JmsTemplate();

    template.setPubSubDomain(true);
    template.setConnectionFactory(connectionFactory());
    template.setDefaultDestinationName(getPubServerUri(null));
    template.setPubSubNoLocal(false);
    template.setReceiveTimeout(100);
    return template;
private TopicConnectionFactory connectionFactory() {

      final TopicConnectionFactory connectionFactory = new ZmqConnectionFactory(
              new String[] { getPubServerUri(null)});

      return connectionFactory;
  }


  private ConnectionFactory connectionFactory() {

      final ConnectionFactory connectionFactory = new ZmqConnectionFactory(
              new String[] { getPubServerUri(null)});

      return connectionFactory;
  }

 private String getPubServerUri(ZMQ_CHANNEL channel)
  {
      return "jms:topic:all?gateway.addr=tcp://*:9714&socket.addr=tcp://*:9714";
  }

Subscriber:

           System.out.println(jmsTemplate.receiveAndConvert());

Tried this myself and I get an Unsupported exception. Not all JMS API was implemented. Will have a look into it.

I have overridden the ZmqConnectionFactory and the ZmQConnection to overcome this:

ZmqConnectionFactory:

public class ZmqConnectionFactory extends org.zeromq.jms.ZmqConnectionFactory {

    private final Map<String, ZmqURI> destinationSchema = new HashMap<String, ZmqURI>();
    private String gatewayFactoryClassName = ZmqGatewayFactory.class.getCanonicalName();
    private String[] packageNameExtensions = null;

    /**
     * Construct Zero MQ connection factory.
     */
    public ZmqConnectionFactory() {
        super();
    }

    /**
     * Construct Zero MQ connection factory with the specified destination URIs.
     * @param destinations  the destination URIs
     */
    public ZmqConnectionFactory(final String[] destinations) {
        super(destinations);
    }

    /**
     * Construct Zero MQ connection factory with the specified destination URIs.
     * @param gatewayFactoryClassName  the class name of the gateway factory
     * @param packageNameExtensions    the array of packages containing extension to ZERO MQ
     * @param destinations             the destination URIs
     */
    public ZmqConnectionFactory(final String gatewayFactoryClassName, final String[] packageNameExtensions, final String[] destinations) {
        super(gatewayFactoryClassName, packageNameExtensions, destinations);
        this.gatewayFactoryClassName = gatewayFactoryClassName;
        this.packageNameExtensions = packageNameExtensions;

    }

    /**
     * @return               return a factory gateway
     * @throws ZmqException  throws exception when factory construction fails
     */
    private ZmqGatewayFactory getFactoryGateway() throws ZmqException {
        try {
            @SuppressWarnings("unchecked")
            final Class<? extends ZmqGatewayFactory> gatewayFactoryClass = (Class<? extends ZmqGatewayFactory>) Class
                    .forName(gatewayFactoryClassName);

            final Constructor<?> gatewayFactoryConstructor = gatewayFactoryClass.getConstructor(String[].class, Map.class);

            final ZmqGatewayFactory gatewayFactory = (ZmqGatewayFactory) gatewayFactoryConstructor.newInstance(packageNameExtensions,
                    destinationSchema);

            return gatewayFactory;
        } catch (ClassNotFoundException ex) {
            throw new ZmqException("Class could not be found.", ex);
        } catch (NoSuchMethodException | SecurityException ex) {
            throw new ZmqException("Unable to find required constructor for class: " + gatewayFactoryClassName, ex);
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            throw new ZmqException("Unable to invoke constructor for class: " + gatewayFactoryClassName, ex);
        }

    }

    @Override
    public QueueConnection createQueueConnection(final String userName, final String password) throws JMSException {
        ZmqGatewayFactory gatewayFactory = getFactoryGateway();
        QueueConnection connection = new ZmqConnection(gatewayFactory, destinationSchema);

        return connection;
    }

    @Override
    public TopicConnection createTopicConnection() throws JMSException {
        return createTopicConnection(null, null);
    }

    @Override
    public TopicConnection createTopicConnection(final String userName, final String password) throws JMSException {

        ZmqGatewayFactory gatewayFactory = getFactoryGateway();
        TopicConnection connection = new ZmqConnection(gatewayFactory, destinationSchema);

        return connection;
    }

    @Override
    public JMSContext createContext() {

       return createContext(null, null, ZmqSession.AUTO_ACKNOWLEDGE);
    }

    @Override
    public JMSContext createContext(final int sessionMode) {

           return createContext(null, null, sessionMode);
    }

    @Override
    public JMSContext createContext(final String userName, final String password) {

           return createContext(userName, password, ZmqSession.AUTO_ACKNOWLEDGE);
    }

    @Override
    public JMSContext createContext(final String userName, final String password, final int sessionMode) {

        try {
            ZmqGatewayFactory gatewayFactory = getFactoryGateway();
            ZmqConnection connection = new ZmqConnection(gatewayFactory, destinationSchema);

            ZmqJMSContext context = new ZmqJMSContext(connection, sessionMode);

            return context;
        } catch (ZmqException ex) {
            throw new JMSRuntimeException("Unable to create context.", ex.getErrorCode(), ex);
        }
    }

ZmqConnection:

public class ZmqConnection extends org.zeromq.jms.ZmqConnection {

    public ZmqConnection(ZmqGatewayFactory gatewayFactory, Map<String, ZmqURI> destinationSchema) {
        super(gatewayFactory, destinationSchema);
        // 
    }

    
    @Override
    public void start() throws JMSException {
    }

    @Override
    public void stop() throws JMSException {
    }
    
    
    @Override
    public void close() throws JMSException {
    }
}

Fixed in release 3.0