Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple Publish-Subscribe pattern using J2SE

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Publish-Subscribe pattern using J2SE

    I am new to Java and was looking into implementing a simple Publish-Subscribe pattern using **only** J2SE and not use any other libraries.

    Since i am pretty new to java, all i have done till now is write 3 classes

    Main:

     
    public class Main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Subscriber s = new Subscriber();
    		Publisher p = new Publisher();
    		p.subscribe(s);
     
    	}
     
    }

    Publisher class:

    public class Publisher {
     
    	/**
    	 * @param args
    	 */
     
    	ArrayList<Subscriber> subscribers = new ArrayList();
     
     
     
    	public void subscribe(Subscriber s)
    	{
    		subscribers.add(s);
    	}
     
    	public void unsubscriber(Subscriber s)
    	{
    		subscribers.remove(s);
    	}
     
    	public void Notify()
    	{
    		for(int i = 0; i< subscribers.size(); i++)
    			subscribers.get(i).update();
    	}
     
    }

    Subscriber class:
    public class Subscriber {
     
    	/**
    	 * @param args
    	 */
    	public void update()
    	{
    		System.out.println("here");
    	}
    }

    I understand that they are basic classes but since most of resources which i have found on the web all point to JMS i wasnt able to move forward and have come here.

    Any pointers on moving forward would be helpful.

    here's some additional info on how the program could work.

    *Suppose the following events are specified in the input:*

    Subscribe,John,ComputerScience
    Subscribe,Sam,Physics
    Publish,Dover,Mathematics
    Publish,PrenticeHall,Literature
    Subscribe,Sam,Literature
    Publish,Dover,Physics
    Subscribe,Mary,ComputerScience
    Publish,PrenticeHall,ComputerScience

    *Then the following results can be expected:*

    Sam notified of a Physics book from Dover
    John notified of a ComputerScience book from PrenticeHall
    Mary notified of a ComputerScience book from PrenticeHall
    I asked this question even in stack overflow but i never received any answer so i thought i could find some pointers here .. thank you for your time.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple Publish-Subscribe pattern using J2SE

    Quote Originally Posted by rya11111 View Post
    ...I asked this question...so i thought i could find some pointers here ...
    Try asking the question here


    On second thought, ask your question in your original post.

    Duplicate thread closed
    Last edited by jps; April 21st, 2013 at 06:47 PM. Reason: Duplicate thread closed

Similar Threads

  1. Simple Publish-Subscribe pattern using J2SE
    By rya11111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2013, 04:31 PM
  2. J2SE for windows ce 6.0
    By djoleasterix in forum Java IDEs
    Replies: 1
    Last Post: March 14th, 2012, 11:25 AM
  3. Javascript -publish on facebookwall
    By Nathiya.k in forum Other Programming Languages
    Replies: 1
    Last Post: September 19th, 2011, 10:19 AM
  4. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  5. Can single MDB subscribe multiple topics?
    By shptlucky in forum Web Frameworks
    Replies: 0
    Last Post: April 22nd, 2011, 05:52 AM