Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Consumer' - 4 code snippet(s) found

 Sample 1. Code Sample / Example / Snippet of javax.jms.MessageConsumer

    public void run() throws JMSException {

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);

connection = factory.createConnection();

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

topic = session.createTopic("topictest.messages");

control = session.createTopic("topictest.control");



MessageConsumer consumer = session.createConsumer(topic);

consumer.setMessageListener(this);



connection.start();



producer = session.createProducer(control);

System.out.println("Waiting for messages...");

}


   Like      Feedback      javax.jms.MessageConsumer


 Sample 2. Display Elements of a List using Java 8 Consumer

List myList = new ArrayList();
      
myList.add("A");
myList.add("B");
myList.add("C");

myList.forEach(System.out::println);

   Like      Feedback     Print elements of a list   java 8  consumer  foreach   list  arraylist  collections


 Sample 3. Usage of Java 8 Consumer interface

Consumer<String> consumer = s->{
   System.out.println(s);
};

consumer.accept("BuggyBread"); // prints BuggyBread

   Like      Feedback     Java 8 Consumer  Consumer interface


 Sample 4. AWS Kinesis Consumer

import com.amazonaws.kinesis.deagg.RecordDeaggregator;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;

public class KinesisConsumer implements RequestHandler<KinesisEvent, Void> {
   
   public Void handleRequest(KinesisEvent event, Context context) {
      System.out.println("Received " + event.getRecords().size() + " Event Records.");
      
      RecordDeaggregator.stream(event.getRecords().stream(), userRecord -> {
         String message = new String(userRecord.getData().array());
         System.out.println(message);
      });

      return null;
   }
}

   Like      Feedback     AWS Kinesis Consumer



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus