当前位置: 动力学知识库 > 问答 > 编程问答 >

implementing publisher and subscriber functionality using activemq-jms from browser(web-client-application(angularjs or javascript or jsp))

问题描述:

i want to implement an application where i should be able to login as a subscriber and i should be able to see the messages/data coming from publisher which am subscribed to.then i should be able to unsubscribe and subscribe to other publishers if i want to.

using JMS,ActiveMQ,SpringBoot i have created a sample standalone application but from here how to connect and communicate to my web application back and forth.

Here is my implemented standalone application code

application.properties

spring.jms.pub-sub-domain=true

#spring.jms.template.default-destination=testTopic

Publisher.java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.stereotype.Component;

@Component

public class Publisher implements CommandLineRunner{

@Autowired

private JmsTemplate jmsTemplate ;

@Autowired

private Topic topic1;

@Autowired

private Topic topic2;

@Override

public void run(String... arg0) throws Exception {

// TODO Auto-generated method stub

Thread.sleep(5000); // wait for subscriptions, unless they are durable

this.jmsTemplate.convertAndSend(this.topic1,"-----> 1st message from publisher -- topic 1");

Thread.sleep(5000);

this.jmsTemplate.convertAndSend(this.topic1,"-----> 2nd message from publisher -- topic 1");

/**

* for topic2

*/

// TODO Auto-generated method stub

Thread.sleep(5000); // wait for subscriptions, unless they are durable

this.jmsTemplate.convertAndSend(this.topic2,"-----> 1st message from publisher -- topic 2");

Thread.sleep(5000);

this.jmsTemplate.convertAndSend(this.topic2,"-----> 2nd message from publisher -- topic 2");

}

}

Subscriber.java

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class Subscriber {

@JmsListener(destination = "Topic1")

public void listener1(String in) {

System.out.println("Listener1: " + in);

}

@JmsListener(destination = "Topic1,Topic2")

public void listener2(String in) {

System.out.println("Listener2: " + in);

}

@JmsListener(destination = "Topic2")

public void listener3(String in) {

System.out.println("Listener3: " + in+"\n listener 3 is just ");

}

}

mainclass : springBootApplication

PubSubJmsBootApplication.java

import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQTopic;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.jms.annotation.EnableJms;

@SpringBootApplication

@EnableJms

public class PubSubJmsBootApplication {

@Bean

public Topic topic1() {

return new ActiveMQTopic("Topic1");

}

@Bean

public Topic topic2() {

return new ActiveMQTopic("Topic2");

}

public static void main(String[] args) {

SpringApplication.run(PubSubJmsBootApplication.class, args);

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: