Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, 29 July 2015

Send Push Notification with Custom Data to iPhone device from Java Server Side

This blog will help you to send Push Notifications with Custom data to iPhone devices from server side where server side code is written in Java.
Follow the below code in order to send Push Notification:
  1. Define the below dependency in your pom.xml, if you are using maven:
<dependency>    <groupId>com.github.fernandospr</groupId>    <artifactId>javapns-jdk16</artifactId>    <version>2.2.1</version></dependency>
Otherwise, put the JavaPNS_2.2.jar into your lib folder of your project.
  1. Send Push Notification with Custom Data to iphone device:
Write the following metthod to send Pust Notification with Custom Data to iphone devices:
FindNerd, known for its quick response time, when people ask questions on various platforms, as it is a good resource of Android, C, iPhone, Javascript, HTML, Java Questions & Answers, etc.


Monday, 13 July 2015

Spring MVC 3 and handling static content

We can handle static content for example: js,css,images in Spring MVC by using mvc:resources element.
mvc:resources element is used to point to the location of static content with a specific public URL pattern.

For example – define the below line in your application-servlet.xml file. This line will serve all requests for static content coming in with a public URL pattern like “/assets/**” by searching in the “/assets/” directory under the root folder in our application.:
<mvc:resources mapping="/assets/**" location="/assets/" />
Here you can find the complete example:
To Read more about Spring MVC 3 and handling static content visit FindNerd.

FindNerd also facilitate its users to ask questions section in which one can post his programming queries on android, php, java, c programming questions and answers etc. Also, we assure to reach you as soon as possible we see it.

Monday, 18 May 2015

Lambda expressions - Java 8

Lambdas?

A lambda is nothing but a anonymous function which accepts zero or more arguments and produces

output. Lambda is basically composed with three components- argument section, an arrow and a

body.

    (argument ...) -> { body } // a valid pseudo code

Example 1 :

     x -> System.out.println(x);

Example 2 :

    (x,y) -> return x + y;

Example 3 :

    (int x, int y) -> { int z = x + y;
    return z;
    }

Let me explain the examples.
First example, shows that brackets are optional if there is only a single argument.
In second example, Type of the arguments are specified. Compiler checks the type if not

declared, it enable runtime typecasting for primitives. Developers can ignore the curly

brackets around a single line body.
Third example shows the argument types and the body of the function.

Lambda expressions can be used to define a inline implementation of a functional interface

i.e. an interface with a single method only. For example - Java developer are well familiar

with the example below -

    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello from thread");
        }
    }).start();

But it could be done more easily with the Lambda expressions as following -

    new Thread(
        () -> System.out.println("Hello from thread")
    ).start();

In the above example, we instantiate the Runable and define the body of run method.

It is very useful in inner class declaration. For example :

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("The button was clicked using old fashion code!");
        }
    });

It can be done as below.

    button.addActionListener( (e) -> {
            System.out.println("The button was clicked. From lambda expressions !");
    });

Here, we don't need to create a inner class to handle the button event.

Lambda expressions are very useful while working with Collections. For example -

    List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    list.forEach(n -> System.out.println(n));

@FunctionalInterface

Similar to the marker interface, there is a concept of functional interface. The interface

with only a single methods are known as functional interface. @FunctionalInterface is a new

interface added in Java 8 to indicate that we can simply use it in our API and take advantage

of Lambda expressions. For example -

    @FunctionalInterface
    public interface MyFunctionalInterface {
        public void doSomeWork();
    }
    public class MyFunctionalInterfaceTest {
        public static void execute(MyFunctionalInterface worker) {
            worker.doSomeWork();
        }
        public static void main(String [] args) {
            execute( () -> System.out.println("Worker invoked using Lambda expression") );
        }
    }

Have fun with Java 8 and Lambda expressions.

Happy coding.

You can see such more blogs at http://findnerd.com/NerdDigest/

Thursday, 7 May 2015

Java 8- Method and Constructor References

Method Referencing

Method reference is feature related to lambda expressions. Method references are more readable

form of Lambda expressions for already written methods. “::” operator is used to define method

reference. It provides a way to a method without executing it. In order to that a method

reference requires a target type context that consists of a compatible functional interface.

Method reference when evaluated creates an instance of the functional interface. The 2 types

of method references are:-

a). Static Method References.

    Syntax:-  ClassName :: methodName

Ex:-

    public interface Parent
    {
            String process(String input);
    }
    public class MethodReference
    {
            public static String toUpperStatic(String input)
            {
                    return input.toUpperCase();
             }
          public static void main(String... args)
            {
                 Parent parent = Parent::toUpperStatic;
         System.out.println("Static reference = "+parent .process("static"));
                 }
    }

b). Instance Method References Of Objects.

    Syntax:-   objRef :: methodName

Ex:-

    public interface Parent
        {
                String process(String input);
        }
        public class MethodReference
        {
                public String toUpperInstance(String input)
                {
                        return input.toUpperCase();
               }
              public static void main(String... args)
                {
                    MethodReference methodReference = new MethodReference();
                         Parent parent  = methodReference::toUpperInstance;
                 System.out.println("Instance reference  "+parent .process("instance"));
                     }
        }

Constructor Referencing

you can create references to the constructors. In Constructor references the method name is

"new" and the receiver is always the name of the class that is defining the constructor. You

can assign a constructor reference to any functional interface which has a method compatible

with the constructor.

    Syntax:-    ClassName :: new

Ex:-

    package com.evon;
        @FunctionalInterface
        public interface MyString
        {
                String strFunc(char[] chArray);
        }
        public class StringCreator
        {
              public static void main(String[] args)
            {
                        MyString mystr = String::new;
                        char[] charArray =

{'e','v','o','n','t','e','c','h','n','o','l','o','g','y'};
                        System.out.println(mystr.strFunc(charArray));
            }
        }

In above example we have assigned a String constructor reference to the functional interface

which we have created just now.

Hope this will help you :)

you can find such more blogs on http://findnerd.com/NerdDigest