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 3 of 3

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 Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Simple Publish-Subscribe pattern using J2SE

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Simple Publish-Subscribe pattern using J2SE

    done. thanks[COLOR="Silver"]

Similar Threads

  1. J2SE for windows ce 6.0
    By djoleasterix in forum Java IDEs
    Replies: 1
    Last Post: March 14th, 2012, 11:25 AM
  2. Javascript -publish on facebookwall
    By Nathiya.k in forum Other Programming Languages
    Replies: 1
    Last Post: September 19th, 2011, 10:19 AM
  3. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  4. Can single MDB subscribe multiple topics?
    By shptlucky in forum Web Frameworks
    Replies: 0
    Last Post: April 22nd, 2011, 05:52 AM
  5. JSR-82 Bluetooth and OBEX with J2SE
    By ryan.jones in forum Java SE APIs
    Replies: 0
    Last Post: March 23rd, 2011, 05:44 AM