tag:blogger.com,1999:blog-90173780410317701102024-03-13T12:51:25.255-07:00Java SolutionsAshish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-9017378041031770110.post-71191348864200902642020-02-22T21:12:00.007-08:002020-02-27T06:15:27.708-08:00Top CSS Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
These CSS interview questions are based on my personal interview experience. Likelihood of question being asked in the interview is from top to bottom.<br />
<br />
<h4 style="text-align: left;">
1. Do you know about Box model ? What are the different values of box-sizing css?</h4>
Margin-border-padding-content<br />
<h4 style="text-align: left;">
2. What are the css precedence rules and css specificity ?</h4>
<div>
Css specificity is the set of rules applied to determine which style is applied to an element.</div>
<div style="text-align: left;">
Order of css specificity from highest to lowest is as follows:-</div>
<div style="text-align: left;">
<br />
<br />
<br />
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">A css rule with !important always takes precedence.</li>
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">If element is having inline style ( element style attribute ) overrides all other styles (1,0,0,0)</li>
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">A css selector using element id (element id attribute) (0,1,0,0)</li>
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">A css selector using class, pseudo class or attribute (0,0,1,0)</li>
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">A css selector using element name (for e.g. h1, p, div) (0,0,0,1)</li>
<li class="li1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 0px;">A css selector appear later in the code override earlier one if both have the same specificity</li>
</div>
<div style="text-align: left;">
Good read to understand how to calculate specificity:- </div>
<div style="text-align: left;">
<a href="https://dev.to/emmabostian/css-specificity-1kca">https://dev.to/emmabostian/css-specificity-1kca</a></div>
<h4 style="text-align: left;">
3. Difference between visibility:hidden; and display:none;</h4>
<div>
<b>visibilty:hidden</b> element is not visible but space is allocated.</div>
<div>
<b>display:none</b> element is not displayed and space is not allocated.</div>
<h4 style="text-align: left;">
4. How to align a block element inside another element.</h4>
<h4 style="text-align: left;">
5. Difference between Static, Relative, Absolute and Fixed position</h4>
<h4 style="text-align: left;">
6. What is shadow dom</h4>
<h4 style="text-align: left;">
7. Pseudo elements</h4>
<div>
::before</div>
<div>
::after</div>
<h4 style="text-align: left;">
8. Pseudo classes</h4>
<div>
:active</div>
<div>
:hover</div>
<div>
:nth-child(n)</div>
<h4 style="text-align: left;">
9. Media query in CSS3</h4>
<div>
<b>10. em vs rem vs px ? Which unit of measurement you should use and when in css ?</b></div>
<br />
<br />
<br />
<br /></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-32505451718078728882020-02-22T20:42:00.002-08:002020-02-27T06:13:05.255-08:00Top Javascript Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
These Javascript interview questions are based on my personal interview experience. Likelihood of question being asked in the interview is from top to bottom.<br />
<br />
<div style="text-align: left;">
</div>
<h4>
1. What is closure and how do we use it ?</h4>
<h4>
2. What is promises and why do we use it ?</h4>
<div>
<div style="text-align: left;">
<b>3. How to use async and await and what problem it solves ?</b></div>
</div>
<h4 style="text-align: left;">
4. Difference between call, apply and bind ?</h4>
<h4 style="text-align: left;">
5. What is Arrow functions and how it is different from normal functions ?</h4>
<h4 style="text-align: left;">
6. Difference between "==" and "===" ?</h4>
<h4 style="text-align: left;">
7. Difference between var, let and const keywords ?</h4>
<h4 style="text-align: left;">
8. Difference between null and undefined ?</h4>
<h4 style="text-align: left;">
9. Object Oriented programming in Javascript - What is constructor and prototype? Do you know about prototypal inheritance. How do you define Class using ES6.</h4>
<h4 style="text-align: left;">
10. What are the methods available in Object -</h4>
<b>create</b> It is used to create a shallow copy of Object. Shallow copy means it copies only the properties and functions of the object but doesn't copy prototype inheritance.<br />
<b>assign</b>- It is used to copy the properties and functions of one object in another object. doesn't copy prototype inheritance.<br />
<b>setPrototypeOf</b><br />
<b>freeze</b>- It is used to freeze object. New properties cannot be added and existing properties cannot be modified.<br />
<b>seal</b>- It is used to seal the schema of object. New properties cannot be added but existing properties can be modified.<br />
<b>defineProperty</b>- It is used to define property. Can make the property readonly using this method.<br />
<h4 style="text-align: left;">
11. Currying functions in javascript?</h4>
<h4 style="text-align: left;">
12. Lexical scoping and variable hoisting in Javascript?</h4>
<div style="text-align: left;">
<br /></div>
<br />
<br /></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-66598205502897129092019-07-04T06:21:00.000-07:002019-07-04T06:21:01.325-07:00Ping Pong using wait notify<div dir="ltr" style="text-align: left;" trbidi="on">
<pre auto="" courier="" monospace="" new="" overflow:="" style="border: 1px dashed #008def; font-size: 13px; line-height: 18px; padding: 15px; text-align: left;"><span style="color: #7b0052; font-weight: bold;">package</span> com.help4j.core.thread;
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">class</span> PingPong <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">void</span> main(String[] args) <span style="color: #d3171b; font-weight: bold;">{</span>
Object LOCK_OBJECT = <span style="color: #7b0052; font-weight: bold;">new</span> Object();
Thread ping = <span style="color: #7b0052; font-weight: bold;">new</span> Thread(<span style="color: #7b0052; font-weight: bold;">new</span> PingPongThread(LOCK_OBJECT, <span style="color: #2a00ff;">"Ping"</span>));
Thread pong = <span style="color: #7b0052; font-weight: bold;">new</span> Thread(<span style="color: #7b0052; font-weight: bold;">new</span> PingPongThread(LOCK_OBJECT, <span style="color: #2a00ff;">"Pong"</span>));
ping.start();
pong.start();
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #7b0052; font-weight: bold;">class</span> PingPongThread <span style="color: #7b0052; font-weight: bold;">implements</span> Runnable<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">private</span> Object LOCK_OBJECT;
<span style="color: #7b0052; font-weight: bold;">private</span> String name;
<span style="color: #7b0052; font-weight: bold;">public</span> PingPongThread(Object LOCK_OBJECT, String name) <span style="color: #d3171b; font-weight: bold;">{</span>
this.LOCK_OBJECT = LOCK_OBJECT;
this.name = name;
<span style="color: #d3171b; font-weight: bold;">}</span>
@Override
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">void</span> run() <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">synchronized</span> (LOCK_OBJECT) <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">while</span>(<span style="color: #7b0052; font-weight: bold;">true</span>) <span style="color: #d3171b; font-weight: bold;">{</span>
System.out.println(name);
<span style="color: #7b0052; font-weight: bold;">try</span> <span style="color: #d3171b; font-weight: bold;">{</span>
Thread.sleep(1000);
<span style="color: #d3171b; font-weight: bold;">}</span> <span style="color: #7b0052; font-weight: bold;">catch</span> (InterruptedException e1) <span style="color: #d3171b; font-weight: bold;">{</span>
e1.printStackTrace();
<span style="color: #d3171b; font-weight: bold;">}</span>
LOCK_OBJECT.notify();
<span style="color: #7b0052; font-weight: bold;">try</span> <span style="color: #d3171b; font-weight: bold;">{</span>
LOCK_OBJECT.wait(1000);
<span style="color: #d3171b; font-weight: bold;">}</span> <span style="color: #7b0052; font-weight: bold;">catch</span> (InterruptedException e) <span style="color: #d3171b; font-weight: bold;">{</span>
e.printStackTrace();
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span></pre>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-21868216223673717402019-06-13T19:53:00.002-07:002019-06-13T19:53:15.184-07:00Virtual DOM vs Shadow DOM<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
DOM</h3>
<div>
DOM stands for Document Object Model which is object based representation of any structured content such as HTML, XML etc. Browser maintains DOM object to render the HTML and any change in the DOM object cause re-rendering of the whole page.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Virtual DOM</h3>
<div>
Concept of Virtual DOM is adapted by popular UI libraries like React and Vue to solve mainly the performance issue. </div>
<div>
Virtual DOM is an in-memory representation of the DOM. Any update in the DOM first applied to Virtual DOM instead of applying directly to actual DOM. Then it compare the changes against actual DOM through a process call "diffing" and apply the changes efficiently to actual DOM by only re-rendering the changed elements.</div>
<div>
<br /></div>
<div>
<h3>
Shadow DOM</h3>
</div>
<div>
Concept of Shadow DOM is natively supported by browser (Not all browsers are currently supporting). Shadow DOM is mostly about encapsulation of the implementation. You can make reusable native web components which follows Shadow DOM concept. Implementation and styling of native web component is hidden within the Shadow DOM and having no impact from the outer DOM.</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com3tag:blogger.com,1999:blog-9017378041031770110.post-5392146574071722262019-05-18T23:19:00.000-07:002019-07-15T08:37:28.863-07:00Kafka Streams<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
<span style="font-size: large;">Stateless operators</span></h3>
<div>
<ul style="text-align: left;">
<li>branch</li>
<li>filter</li>
<li>inverseFilter</li>
<li>flatMap</li>
<li>flatMapValues</li>
<li>foreach</li>
<li>groupByKey</li>
<li>groupBy</li>
<li>map</li>
<li>mapValues</li>
</ul>
<h3 style="text-align: left;">
<span style="font-size: large;">Stateful operators</span></h3>
</div>
<div>
<ul style="text-align: left;">
<li>join</li>
<li>aggregate</li>
<li>count</li>
<li>reduce</li>
<li>windowing</li>
</ul>
<h3 style="text-align: left;">
<span style="font-size: large;">Window</span></h3>
</div>
<div>
<ol style="text-align: left;">
<li>Tumbling window</li>
<ul>
<li>Time based, Fixed Size, Non overlapping, Gap less windows</li>
<li>For e.g. if window-size=5min and advance-interval =5min then it looks like [0-5min] [5min-10min] [10min-15min].....</li>
</ul>
<li>Hopping window</li>
<ul>
<li>Time based, Fixed Size, Overlapping windows</li>
<li>For e.g. if widow-size=5min and advance-interval=3min then it looks like [0-5min] [3min-8min] [6min-11min]......</li>
</ul>
<li>Sliding window</li>
<ul>
<li>Fixed size overlapping window that works on the difference between record timestamp</li>
<li>Used only for join operation</li>
</ul>
<li>Session window</li>
<ul>
<li>Session based, Dynamically sized, Non overlapping, Data driven window.</li>
<li>Used to aggregate key based events into session.</li>
</ul>
</ol>
<div>
For more information on windowing, refer <a href="https://kafka.apache.org/20/documentation/streams/developer-guide/dsl-api.html#windowing" target="_blank">Apache Kafka Documentation</a></div>
</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com4tag:blogger.com,1999:blog-9017378041031770110.post-78831419165041170052019-05-12T00:05:00.002-07:002019-05-12T01:06:05.584-07:00Confluent Schema Registry<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div>
<h2 style="text-align: left;">
<span style="color: #134f5c;">Avro</span></h2>
</div>
<h4 style="text-align: left;">
<span style="color: #134f5c;">Primitive Types</span></h4>
<div>
<ol style="text-align: left;">
<li><span style="color: #444444;">null</span></li>
<li><span style="color: #444444;">boolean</span></li>
<li><span style="color: #444444;">int (32 bit)</span></li>
<li><span style="color: #444444;">long (64 bit)</span></li>
<li><span style="color: #444444;">float (32 bit)</span></li>
<li><span style="color: #444444;">double (64 bit)</span></li>
<li><span style="color: #444444;">byte[] (8 bit)</span></li>
<li><span style="color: #444444;">string (char squence)</span></li>
</ol>
<h4 style="text-align: left;">
<span style="color: #134f5c;">Complex Types</span></h4>
</div>
<div>
<ol style="text-align: left;">
<li><span style="color: #444444;">record</span></li>
<li><span style="color: #444444;">enum</span></li>
<li><span style="color: #444444;">array</span></li>
<li><span style="color: #444444;">map</span></li>
<li><span style="color: #444444;">union</span></li>
<li><span style="color: #444444;">fixed</span></li>
</ol>
<h4 style="text-align: left;">
<span style="color: #134f5c;">Avro Schema Definition</span></h4>
</div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">namespace (required)</span></li>
<li><span style="color: #444444;">type (required) => record, enum, array, map, union, fixed</span></li>
<li><span style="color: #444444;">name (required)</span></li>
<li><span style="color: #444444;">doc (optional)</span></li>
<li><span style="color: #444444;">aliases (optional)</span></li>
<li><span style="color: #444444;">fields (required)</span></li>
<ul>
<li><span style="color: #444444;">name (required)</span></li>
<li><span style="color: #444444;">type (required)</span></li>
<li><span style="color: #444444;">doc (optional)</span></li>
<li><span style="color: #444444;">default (optional)</span></li>
<li><span style="color: #444444;">order (optional)</span></li>
<li><span style="color: #444444;">aliases (optional)</span></li>
</ul>
</ul>
<h2 style="text-align: left;">
<span style="color: #134f5c;">Confluent Schema Registry</span></h2>
</div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Schema Registry stores all schemas in a Kafka topic defined by kafkastore.config=<b>_schemas </b>(default)<b> </b>which is a single partition topic with log compacted.</span></li>
<li><span style="color: #444444;">The default response media type application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json are used in response header.</span></li>
<li><span style="color: #444444;"><b>HTTP and HTTPS</b> client protocol are supported for schema registry.</span></li>
<li><span style="color: #444444;">Prefix to apply to metric names for the default JMX reporter <b>kafka.schema.registry</b></span></li>
<li><span style="color: #444444;">Default port for listener is 8081</span></li>
<li><span style="color: #444444;">Confluent support primitive types of null, Boolean, Integer, Long, Float, Double, String, byte[], and complex type of IndexedRecord. Sending data of other types to KafkaAvroSerializer will cause a SerializationException</span></li>
</ul>
</div>
<div>
<br /></div>
<h2 style="text-align: left;">
<span style="color: #134f5c;">Schema Compatibility Types</span></h2>
<div>
<ol style="text-align: left;">
<li><span style="color: #444444;">BACKWARD</span></li>
<ul>
<li><span style="color: #444444;">Consumer using schema X can process data produced with schema X or X-1. In case of BACKWARD_TRANSITIVE, consumer using schema X can process data produced with all previous schema X, X-1, X-2 and so on</span></li>
<li><span style="color: #444444;">Delete field without default value (Required field) is allowed. In this case, Consumer ignore this field.</span></li>
<li><span style="color: #444444;">Add field with default value (Optional field) is allowed. In this case, Consumer will assign default value.</span></li>
<li><span style="color: #444444;">BACKWARD is default compatibility type in confluent schema registry.</span></li>
<li><span style="color: #444444;">There is no assurance that consumers using older schema can read data produced using the new schema. Therefore, upgrade all consumers before you start producing new events.</span></li>
</ul>
<li><span style="color: #444444;">FORWARD</span></li>
<ul>
<li><span style="color: #444444;">Data produced using schema X can be ready by consumers with schema X or X-1. In case of FORWARD_TRANSITIVE, data produced using schema X can be ready by consumers with all previous schema X, X-1, X-2 and so on</span></li>
<li><span style="color: #444444;">Add field without default value (Required field) is allowed. In this case, Consumer ignore this field.</span></li>
<li><span style="color: #444444;">Delete field with default value (Optional field) is allowed. In this case, Consumer will assign default value.</span></li>
<li><span style="color: #444444;">There is no assurance that consumers using the new schema can read data produced using older schema. Therefore, first upgrade all producers to using the new schema and make sure the data already produced using the older schema are not available to consumers, then upgrade the consumers.</span></li>
</ul>
<li><span style="color: #444444;">FULL</span></li>
<ul>
<li><span style="color: #444444;">Backward and forward compatible between schema X and X-1. In case of FULL_TRANSITIVE, backward and forward compatible between all previous schema X and X-1 and X-2 and so on</span></li>
<li><span style="color: #444444;">Modify field with default value (Optional field) is allowed.</span></li>
<li><span style="color: #444444;">There are assurances that consumers using older schema can read data produced using the new schema and that consumers using the new schema can read data produced using older schema. Therefore, you can upgrade the producers and consumers independently.</span></li>
</ul>
<li><span style="color: #444444;">NONE</span></li>
<ul>
<li><span style="color: #444444;">Compatibility type means schema compatibility checks are disabled.</span></li>
<li><span style="color: #444444;">Upgrading Consumer or Producer depends. For example, modifying a field type from Number to String. In this case, you will either need to upgrade all producers and consumers to the new schema version at the same time</span></li>
</ul>
</ol>
<div>
<br /></div>
</div>
<div>
<br /></div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-84574914672114064192019-05-11T20:45:00.003-07:002019-05-11T20:45:55.588-07:00Kafka Consumer Using Java<div dir="ltr" style="text-align: left;" trbidi="on">
<pre auto="" courier="" monospace="" new="" overflow:="" style="border: 1px dashed #008def; font-size: 13px; line-height: 18px; padding: 15px; text-align: left;"><span style="color: #7b0052; font-weight: bold;">package</span> com.abc.demo;
<span style="color: #7b0052; font-weight: bold;">import</span> java.time.Duration;
<span style="color: #7b0052; font-weight: bold;">import</span> java.util.Collections;
<span style="color: #7b0052; font-weight: bold;">import</span> java.util.Properties;
<span style="color: #7b0052; font-weight: bold;">import</span> java.util.concurrent.ExecutionException;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.consumer.ConsumerConfig;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.consumer.ConsumerRecord;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.consumer.ConsumerRecords;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.consumer.KafkaConsumer;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.common.serialization.StringDeserializer;
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">class</span> KafkaConsumerTest <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">void</span> main(String[] args) <span style="color: #7b0052; font-weight: bold;">throws</span> InterruptedException, ExecutionException<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #3f7f5f;">//Create consumer property
</span> String bootstrapServer = <span style="color: #2a00ff;">"localhost:9092"</span>;
String groupId = <span style="color: #2a00ff;">"my-first-consumer-group"</span>;
String topicName = <span style="color: #2a00ff;">"my-first-topic"</span>;
Properties properties = <span style="color: #7b0052; font-weight: bold;">new</span> Properties();
properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId);
properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, <span style="color: #2a00ff;">"earliest"</span>);
properties.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, <span style="color: #2a00ff;">"false"</span>);
<span style="color: #3f7f5f;">//Create consumer
</span> KafkaConsumer<String, String> consumer = <span style="color: #7b0052; font-weight: bold;">new</span> KafkaConsumer<>(properties);
<span style="color: #3f7f5f;">//Subscribe consumer to topic(s)
</span> consumer.subscribe(Collections.singleton(topicName));
<span style="color: #3f7f5f;">//Poll for new data
</span> <span style="color: #7b0052; font-weight: bold;">while</span>(<span style="color: #7b0052; font-weight: bold;">true</span>)<span style="color: #d3171b; font-weight: bold;">{</span>
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
<span style="color: #7b0052; font-weight: bold;">for</span>(ConsumerRecord<String, String> record: records)<span style="color: #d3171b; font-weight: bold;">{</span>
System.out.println(record.key() + record.value());
System.out.println(record.topic() + record.partition() + record.offset());
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #3f7f5f;">//Commit consumer offset manually (recommended)
</span> consumer.commitAsync();
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span></pre>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-39920946409513596062019-05-11T03:44:00.000-07:002019-05-11T03:44:04.317-07:00Kafka Producer Using Java<div dir="ltr" style="text-align: left;" trbidi="on">
<pre auto="" courier="" monospace="" new="" overflow:="" style="border: 1px dashed #008def; font-size: 13px; line-height: 18px; padding: 15px; text-align: left;"><span style="color: #7b0052; font-weight: bold;">package</span> com.abc.demo;
<span style="color: #7b0052; font-weight: bold;">import</span> java.util.Properties;
<span style="color: #7b0052; font-weight: bold;">import</span> java.util.concurrent.ExecutionException;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.producer.KafkaProducer;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.producer.ProducerConfig;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.clients.producer.ProducerRecord;
<span style="color: #7b0052; font-weight: bold;">import</span> org.apache.kafka.common.serialization.StringSerializer;
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">class</span> KafkaProducerTest <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">void</span> main(String[] args) <span style="color: #7b0052; font-weight: bold;">throws</span> InterruptedException, ExecutionException<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #3f7f5f;">//Create producer property
</span> String bootstrapServer = <span style="color: #2a00ff;">"localhost:9092"</span>;
Properties properties = <span style="color: #7b0052; font-weight: bold;">new</span> Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
<span style="color: #3f7f5f;">//Create safe producer
</span> properties.setProperty(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, <span style="color: #2a00ff;">"true"</span>);
properties.setProperty(ProducerConfig.ACKS_CONFIG, <span style="color: #2a00ff;">"all"</span>);
properties.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, <span style="color: #2a00ff;">"5"</span>);
properties.setProperty(ProducerConfig.RETRIES_CONFIG, Integer.toString(Integer.MAX_VALUE));
<span style="color: #3f7f5f;">//High throughput producer (at the expense of a bit of latency and CPU usage)
</span> properties.setProperty(ProducerConfig.COMPRESSION_TYPE_CONFIG, <span style="color: #2a00ff;">"snappy"</span>);
properties.setProperty(ProducerConfig.LINGER_MS_CONFIG, <span style="color: #2a00ff;">"20"</span>); <span style="color: #3f7f5f;">//20ms wait time
</span> properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, Integer.toString(32*1024)); <span style="color: #3f7f5f;">//32KB batch size
</span>
<span style="color: #3f7f5f;">//Create producer
</span> KafkaProducer<String, String> producer = <span style="color: #7b0052; font-weight: bold;">new</span> KafkaProducer<>(properties);
<span style="color: #3f7f5f;">//create a producer record
</span> ProducerRecord<String, String> record = <span style="color: #7b0052; font-weight: bold;">new</span> ProducerRecord<>(<span style="color: #2a00ff;">"topicName"</span>, <span style="color: #2a00ff;">"firstRecord"</span>);
<span style="color: #3f7f5f;">//create producer record with key
</span> <span style="color: #3f7f5f;">//new ProducerRecord<>("topicName", "MessageKey", "Message");
</span> <span style="color: #3f7f5f;">//create producer record with key and partition number
</span> <span style="color: #3f7f5f;">//new ProducerRecord<>("topicName", 1 /*partition number*/, "MessageKey", "Message");
</span>
<span style="color: #3f7f5f;">//send data - asynchronous
</span> <span style="color: #3f7f5f;">//without callback
</span> <span style="color: #3f7f5f;">//producer.send(record);
</span> <span style="color: #3f7f5f;">//with callback
</span> producer.send(record, (recordMetadata, exception) -> <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">if</span>(exception == <span style="color: #7b0052; font-weight: bold;">null</span>)<span style="color: #d3171b; font-weight: bold;">{</span>
System.out.println(recordMetadata.topic() + <span style="color: #2a00ff;">"+"</span> + recordMetadata.partition() + <span style="color: #2a00ff;">"+"</span> + recordMetadata.offset());
<span style="color: #d3171b; font-weight: bold;">}</span><span style="color: #7b0052; font-weight: bold;">else</span><span style="color: #d3171b; font-weight: bold;">{</span>
System.err.println(exception.getMessage());
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>);
<span style="color: #3f7f5f;">//send data - synchronous
</span> <span style="color: #3f7f5f;">//without callback
</span> <span style="color: #3f7f5f;">//producer.send(record).get(); //.get() make it synchronous call
</span>
<span style="color: #3f7f5f;">//flush data
</span> producer.flush();
<span style="color: #3f7f5f;">//flush and close producer
</span> producer.close();
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span></pre>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-28759114118703248882019-03-30T19:34:00.003-07:002020-04-07T18:29:08.303-07:00Apache Kafka CCDAK Exam Notes<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;">
<div style="text-align: left;">
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;">Hi Readers,</span><br />
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #3d85c6; font-family: "trebuchet ms" , sans-serif;"><a href="https://codingnconcepts.com/post/apache-kafka-ccdak-exam-notes/" style="background-color: yellow;" target="_blank">click here for updated kafka notes</a></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #444444;"><br /></span><span style="color: #444444;">If you are planning or preparing for Apache Kafka Certification then this is the right place for you.</span><span style="color: #444444;">There are many Apache Kafka Certifications are available in the market but CCDAK (Confluent Certified Developer for Apache Kafka) is the most known certification as Kafka is now maintained by Confluent.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #444444;"><br /></span><span style="color: #444444;"><span style="background-color: white;">Confluent has introduced CCOAK certification recently. CCOAK is mainly for devOps engineer focusing on build and manage Kafka cluster. CCDAK is mainly for developers and Solution architects focusing on design, producer and consumer. If you are still not sure, I recommend to go for CCDAK as it is more comprehensive exam as compared to CCOAK.</span></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #444444;"><span style="background-color: white;"><br /></span></span><span style="color: #444444;">From here onward, we will talk about how to prepare for CCDAK.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #444444;"><br /></span><span style="color: #444444;"><span style="color: #444444;">I have recently </span><a href="https://www.credential.net/qaojuywx" target="_blank"><span style="color: #3d85c6;">cracked</span></a><span style="color: #444444;"> CCDAK and would suggest that prepare well for the exam. This exam verify your theoretical as well as practical understanding of Kafka. You need to answer 60 questions in 90 minutes from your laptop under the supervision of online proctor. There is no mention of number of questions need to be correct in order to pass the exam. They just tell you pass or fail at the end of exam. At least 40-50 hours of preparation is required.</span></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #444444;"><span style="color: #444444;"><br /></span></span><span style="color: #444444;">I have prepared for CCDAK using following:</span></span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span style="color: #444444; font-family: "times" , "times new roman" , serif; font-size: small;"><br /></span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<div style="text-align: left;">
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"><span style="font-family: "times" , "times new roman" , serif;"> </span><span style="font-family: "trebuchet ms" , sans-serif;">1. </span><a href="https://kafka.apache.org/documentation/" style="font-family: "trebuchet ms", sans-serif;" target="_blank"><span style="color: #3d85c6;">Apacha Kafka Documentation</span></a></span></div>
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"> 2. <a href="https://docs.confluent.io/current/index.html" target="_blank"><span style="color: #3d85c6;">Confluent Kafka Documentation</span></a></span><br />
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"> 3. <a href="https://www.confluent.io/wp-content/uploads/confluent-kafka-definitive-guide-complete.pdf" target="_blank"><span style="color: #3d85c6;">Confluent Kafka Definitive Guide PDF</span></a></span><br />
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"> 4. <a href="https://www.udemy.com/apache-kafka-tutorial-for-beginners/" target="_blank"><span style="color: #3d85c6;">Udemy Apache Kafka course for beginners</span></a></span><br />
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;"> 5. <a href="https://www.udemy.com/confluent-certified-developer-for-apache-kafka/" target="_blank"><span style="color: #3d85c6;">Udemy CCDAK 150 practice exam questions!</span></a></span></div>
<br />
<br />
<div style="text-align: left;">
<div style="text-align: left;">
<div style="text-align: left;">
<span style="color: #444444; font-family: "trebuchet ms" , sans-serif;">You should prepare well in following areas:</span></div>
</div>
</div>
<div style="text-align: left;">
</div>
<ol style="text-align: left;">
<li>Kafka Architecture</li>
<ul>
<li>Read Confluent Kafka Definitive Guide PDF</li>
<li>Read Apache Kafka Documentation</li>
<li>Once you read all these, revise using <span style="color: orange;">KAFKA THEORY</span> section in this blog. You can expect most of the questions from these notes.</li>
</ul>
<li>Kafka Java APIs</li>
<ul>
<li>Read Apache Kafka Documentation how to create producer and consumer in Java</li>
</ul>
<li>Kafka CLI</li>
<ul>
<li>Read Confluent Kafka Definitive Guide PDF</li>
<li>Once you read all these, revise using <span style="color: #6aa84f;">KAFKA CLI</span><span style="color: #b6d7a8;"> </span>section in this blog. You can expect most of the questions from these notes.</li>
</ul>
<li>Kafka Streams</li>
<ul>
<li>Read Confluent Kafka Definitive Guide PDF</li>
</ul>
<li>Kafka Monitoring (Metrics)</li>
<ul>
<li>Read Confluent Kafka Definitive Guide PDF</li>
<li>Read Apache Kafka Documentation for important metrics</li>
<li>Read Confluent Kafka Documentation as well</li>
</ul>
<li>Kafka Security</li>
<ul>
<li>Read Apache Kafka Documentation</li>
</ul>
<li>Confluent KSQL</li>
<ul>
<li>Read about KSQL from Confluent Documentation</li>
</ul>
<li>Confluent REST Proxy</li>
<ul>
<li>Read about KSQL from Confluent Documentation</li>
</ul>
<li>Confluent Schema Registry</li>
<ul>
<li>Read about Schema Registry from Confluent Documentation</li>
<li>You can expect most of the questions regarding <a href="https://lahotisolutions.blogspot.com/2019/05/confluent-schema-registry.html" target="_blank">Confluent Schema Registry from these notes.</a></li>
</ul>
</ol>
<br />
<h3 style="text-align: left;">
<span style="color: #444444; font-size: large;">Questions from CCDAK Exam</span></h3>
<ol style="text-align: left;">
<li>Kafka Theory</li>
<ul>
<li>Kafka is a .... ? <b>pub-sub system</b></li>
<li>Mostly Kafka is written in which language? <b>Scala</b></li>
</ul>
<li>Kafka Streams <a href="https://lahotisolutions.blogspot.com/2019/05/kafka-streams.html" target="_blank">(Read Kafka Streams notes to get answers of below questions)</a></li>
<ul>
<li>Which of the Kafka Stream operators are stateful ?</li>
<li>Which of the Kafka Stream operators are stateless ?</li>
<li>Which window is not having gap ?</li>
</ul>
<li>Confluent Schema Registry <a href="https://lahotisolutions.blogspot.com/2019/05/confluent-schema-registry.html" target="_blank">(Read Confluent Schema Registry notes to get answers of below questions)</a></li>
<ul>
<li>Which of the following is not a primitive type of Avro ?</li>
<li>Which of the following in not a complex type of Avro?</li>
<li>Which of the following is not a required field in Avro Schema?</li>
<li>Delete a field without default value in Avro schema is ...... compatibility? </li>
</ul>
</ol>
<br />
<strong><span style="color: orange; font-size: large;">KAFKA THEORY</span></strong><br />
<strong style="color: #444444;"><span style="font-size: large;"><br /></span></strong><span style="color: #444444;">1. Cluster</span><br />
<span style="color: #444444;">2. Rack</span><br />
<span style="color: #444444;">3. Broker</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Every broker in Kafka is a "bootstrap server" which knows about all brokers, topics and partitions (metadata) that means Kafka client (e.g. producer,consumer etc) only need to connect to one broker in order to connect to entire cluster.</span></li>
<li><span style="color: #444444;">At all times, only one broker should be the controller, and one broker must always be the controller in the cluster</span></li>
</ul>
<span style="color: #444444;">4. Topic</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Kafka takes bytes as input without even loading them into memory (that's called zero copy)</span></li>
<li><span style="color: #444444;">Brokers have defaults for all the topic configuration parameters</span> </li>
</ul>
<span style="color: #444444;">5. Partition</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Topic can have one or more partition.</span></li>
<li><span style="color: #444444;">It is not possible to delete a partition of topic once created.</span></li>
<li><span style="color: #444444;">Order is guaranteed within the partition and once data is written into partition, its immutable!</span></li>
<li><span style="color: #444444;">If producer writes at 1 GB/sec and consumer consumes at 250MB/sec then requires 4 partition!</span></li>
</ul>
<div>
<span style="color: #444444;">6. Segment</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Partitions are made of segments (.log files)</span></li>
<li><span style="color: #444444;">At a time only one segment is active in a partition</span></li>
<li><span style="color: #444444;"><b>log.segment.bytes=1 GB (default)</b> Max size of a single segment in bytes</span></li>
<li><span style="color: #444444;"><b>log.segment.ms=1 week (default)</b> Time kafka will wait before closing the segment if not full</span></li>
<li><span style="color: #444444;">Segment come with two indexes (files)</span></li>
<ul>
<li><span style="color: #444444;">An offset to position index (.index file): Allows kafka where to read to find a message</span></li>
<li><span style="color: #444444;">A timestamp to offset index (.timeindex file): Allows kafka to find a message with a timestamp</span></li>
</ul>
<li><span style="color: #444444;"><b>log.cleanup.policy=delete (Kafka default for all user topics)</b> Delete data based on age of data (default is 1 week)</span></li>
<li><span style="color: #444444;"><b>log.cleanup.policy=compact</b> Delete based on keys of your messages. Will delete old duplicate keys after the active segment is committed. (Kafka default for topic __consumer_offsets)</span></li>
<li><span style="color: #444444;">Log cleanup happen on partition segments. Smaller/more segments means the log cleanup will happen more often!</span></li>
<li><span style="color: #444444;">The cleaner checks for work every 15 seconds (log.cleaner.backoff.ms) </span></li>
<li><span style="color: #444444;"><b>log.retention.hours= 1 week (default)</b> number of hours to keep data for</span></li>
<li><span style="color: #444444;"><b>log.retention.bytes = -1 (infinite default)</b> max size in bytes for each partition</span></li>
<li><span style="color: #444444;">Old segments will be deleted based on log.retention.hours or log.retention.bytes rule</span></li>
<li><span style="color: #444444;">The offset of message is immutable.</span></li>
<li><span style="color: #444444;">Deleted records can still be seen by consumers for a period of delete.retention.ms=24 hours (default)</span></li>
</ul>
</div>
<span style="color: #444444;">7. Offset</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Partition is having its own offset starting from 0.</span></li>
</ul>
<span style="color: #444444;">8. Topic Replication</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Replication factor = 3 and partition = 2 means there will be total 6 partition distributed across Kafka cluster. Each partition will be having 1 leader and 2 ISR (in-sync replica).</span></li>
<li><span style="color: #444444;">Broker contains leader partition called leader of that partition and only leader can receive and serve data for partition.</span></li>
<li><span style="color: #444444;">Replication factor can not be greater then number of broker in the kafka cluster. If topic is having a replication factor of 3 then each partition will live on 3 different brokers.</span></li>
</ul>
<span style="color: #444444;">9. Producer</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Automatically recover from errors: LEADER_NOT_AVAILABLE, NOT_LEADER_FOR_PARTITION, REBALANCE_IN_PROGRESS</span></li>
<li><span style="color: #444444;">Non retriable errors: MESSAGE_TOO_LARGE</span></li>
<li><span style="color: #444444;">When produce to a topic which doesn't exist and auto.create.topic.enable=true then kafka creates the topic automatically with the broker/topic settings num.partition and default.replication.factor</span></li>
</ul>
<span style="color: #444444;">10. Producer Acknowledgment</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><b>acks=0</b>: Producer do not wait for ack (possible data loss)</span></li>
<li><span style="color: #444444;"><b>acks=1</b>: Producer wait for leader ack (limited data loss)</span></li>
<li><span style="color: #444444;"><b>acks=all</b>: Producer wait for leader+replica ack (no data loss)</span></li>
</ul>
<span style="color: #444444;">acks=all must be used in conjunction with min.insync.replicas (can be set at broker or topic level)</span><br />
<span style="color: #444444;">min.insync.replica=2 implies that at least 2 brokers that are ISR(including leader) must acknowledge</span><br />
<span style="color: #444444;">e.g. replication.factor=3, min.insync.replicas=2,acks=all can only tolerate 1 broker going down, otherwise the producer will receive an exception NOT_ENOUGH_REPLICAS on send</span><br />
<span style="color: #444444;"><br /></span><span style="color: #444444;">11. Safe Producer Config</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><strong>min.insync.replicas=2</strong> (set at broker or topic level)</span></li>
<li><span style="color: #444444;"><strong>retries=MAX_INT</strong>: number of reties by producer in case of transient failure/exception. (default is 0)</span></li>
<li><span style="color: #444444;"><strong>max.in.flight.per.connection number=5:</strong> number of producer request can be made in parellel (default is 5)</span></li>
<li><span style="color: #444444;"><strong>acks=all</strong></span></li>
<li><span style="color: #444444;"><strong>enable.idempotence=true</strong>: producer send producerId with each message to identify for duplicate msg at kafka end. When kafka receives duplicate message with same producerId which it already committed. It do not commit it again and send ack to producer (default is false)</span></li>
</ul>
<span style="color: #444444;">12. High Throughput Producer using compression and batching</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><strong>compression.type=snappy</strong>: value can be none(default), gzip, lz4, snappy. Compression is enabled at the producer level and doesn't require any config change in broker or consumer Compression is more effective in case of bigger batch of messages being sent to kafka</span></li>
<li><span style="color: #444444;"><strong>linger.ms=20</strong>:Number of millisecond a producer is willing to wait before sending a batch out. (default 0). Increase linger.ms value increase the chance of batching.</span></li>
<li><span style="color: #444444;"><strong>batch.size=32KB or 64KB</strong>: Maximum number of bytes that will be included in a batch (default 16KB). Any message bigger than the batch size will not be batched</span></li>
</ul>
<span style="color: #444444;">10. Message Key</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Producer can choose to send a key with message. </span></li>
<li><span style="color: #444444;">If key= null, data is send in round robin</span></li>
<li><span style="color: #444444;">If key is sent, then all message for that key will always go to same partition. This can be used to order the messages for a specific key since order is guaranteed in same partition.</span></li>
<li><span style="color: #444444;">Adding a partition to the topic will loose the guarantee of same key go to same partition.</span></li>
<li><span style="color: #444444;">Keys are hashed using "murmur2" algorithm by default.</span></li>
</ul>
<span style="color: #444444;">13. Consumer</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Per thread one consumer is the rule. Consumer must not be multi threaded.</span></li>
<li><span style="color: #444444;"><b>records-lag-max </b>(monitoring metrics) The maximum lag in terms of number of records for any partition in this window. An increasing value over time is your best indication that the consumer group is not keeping up with the producers.</span></li>
</ul>
<span style="color: #444444;">14. Consumer Group</span><br />
<span style="color: #444444;"><br /></span><span style="color: #444444;">15. Consumer Offset</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">When consumer in a group has processed the data received from Kafka, it commits the offset in Kafka topic named _consumer_offset which is used when a consumer dies, it will be able to read back from where it left off.</span></li>
</ul>
<span style="color: #444444;">14. Delivery Semantics</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><b>At most once</b> : Offset are committed as soon as message batch is received. If the processing goes wrong, the message will be lost (it won't be read again)</span></li>
<li><span style="color: #444444;"><b>At least once</b> <b>(default)</b>: Offset are committed after the message is processed.If the processing goes wrong, the message will be read again. This can result in duplicate processing of message. Make sure your processing is idempotent. (i.e. re-processing the message won't impact your systems). For most of the application, we use this and ensure processing are idempotent.</span></li>
<li><span style="color: #444444;"><b>Exactly once</b>: Can only be achieved for Kafka=>Kafka workflows using Kafka Streams API. For Kafka=>Sink workflows, use an idempotent consumer.</span></li>
</ul>
<div>
<span style="color: #444444;">16. Consumer Offset commit strategy</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;"><b>enable.auto.commit=true & synchronous processing of batches</b>: with auto commit, offset will be committed automatically for you at regular interval (auto.commit.interval.ms=5000 by default) every time you call .poll(). If you don't use synchronous processing, you will be in "at most once" behavior because offsets will be committed before your data is processed.</span></li>
<li><span style="color: #444444;"><b>enable.auto.commit=false & manual commit of offsets (recommended)</b></span></li>
</ul>
<div>
<span style="color: #444444;">17. Consumer Offset reset behavior</span></div>
</div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;"><b>auto.offset.reset=latest</b>: will read from the end of the log</span></li>
<li><span style="color: #444444;"><b>auto.offset.reset=earliest</b>: will read from the start of the log</span></li>
<li><span style="color: #444444;"><b>auto.offset.reset=none</b>: will throw exception of no offset is found</span></li>
<li><span style="color: #444444;">Consumer offset can be lost if hasn't read new data in 7 days. This can be controlled by broker setting offset.retention.minutes </span></li>
</ul>
</div>
<div>
<span style="color: #444444;">18. Consumer Poll Behavior</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;"><b>fetch.min.bytes = 1 (default)</b>: Control how much data you want to pull at least on each request. Help improving throughput and decreasing request number. At the cost of latency.</span></li>
<li><span style="color: #444444;"><b>max.poll.records = 500 (default)</b>: Controls how many records to receive per poll request. Increase if your messages are very small and have a lot of available RAM.</span></li>
<li><span style="color: #444444;"><b>max.partition.fetch.bytes = 1MB (default)</b>: Maximum data returned by broker per partition. If you read from 100 partition, you will need a lot of memory (RAM)</span></li>
<li><span style="color: #444444;"><b>fetch.max.bytes = 50MB (default)</b>: Maximum data returned for each fetch request (covers multiple partition). Consumer performs multiple fetches in parallel.</span> </li>
</ul>
</div>
<span style="color: #444444;">19. Consumer Heartbeat Thread</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Heartbeat mechanism is used to detect if consumer application in dead.</span></li>
<li><span style="color: #444444;"><b>session.timeout.ms=10 seconds (default)</b> If heartbeat is not sent in 10 second period, the consumer is considered dead. Set lower value to faster consumer rebalances</span></li>
<li><span style="color: #444444;"><b>heartbeat.interval.ms=3 seconds (default) </b>Heartbeat is sent in every 3 seconds interval. Usually 1/3rd of session.timeout.ms</span></li>
</ul>
<div>
<span style="color: #444444;">20. Consumer Poll Thread</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Poll mechanism is also used to detect if consumer application is dead.</span></li>
<li><span style="color: #444444;"><b>max.poll.interval.ms = 5 minute (default)</b> Max amount of time between two .poll() calls before declaring consumer dead. If processing of message batch takes more time in general in application then should increase the interval.</span></li>
</ul>
</div>
<span style="color: #444444;">21. Kafka Guarantees</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">Messages are appended to a topic-partition in the order they are sent</span></li>
<li><span style="color: #444444;">Consumer read the messages in the order stored in topic-partition</span></li>
<li><span style="color: #444444;">With a replication factor of N, producers and consumers can tolerate upto N-1 brokers being down</span></li>
<li><span style="color: #444444;">As long as number of partitions remains constant for a topic ( no new partition), the same key will always go to same partition</span></li>
</ul>
<span style="color: #444444;">22. Client Bi-Directional Compatibility</span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;">an Older client (1.1) can talk to Newer broker (2.0)</span></li>
<li><span style="color: #444444;">a Newer client (2.0) can talk to Older broker (1.1)</span></li>
</ul>
<div>
<span style="color: #444444;">23. Kafka Connect</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Source connect: Get data from common data source to Kafka</span></li>
<li><span style="color: #444444;">Sink connect: Publish data from Kafka to common data source</span></li>
</ul>
<div>
<span style="color: #444444;">24. Zookeeper</span></div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">ZooKeeper servers will be deployed on multiple nodes. This is called an ensemble. An ensemble is a set of 2n + 1 ZooKeeper servers where n is any number greater than 0. The odd number of servers allows ZooKeeper to perform majority elections for leadership. At any given time, there can be up to n failed servers in an ensemble and the ZooKeeper cluster will keep quorum. If at any time, quorum is lost, the ZooKeeper cluster will go down. </span></li>
<li><span style="color: #444444;">In Zookeeper multi-node configuration, <b>initLimit </b>and <b>syncLimit </b>are used to govern how long following ZooKeeper servers can take to initialize with the current leader and how long they can be out of sync with the leader. </span></li>
<ul>
<li><span style="color: #444444;">If tickTime=2000, initLimit=5 and syncLimit=2 then a follower can take (tickTime*initLimit) = 10000ms to initialize and may be out of sync for up to (tickTime*syncLimit) = 4000ms</span></li>
</ul>
<li><span style="color: #444444;">In Zookeeper multi-node configuration, The server.* properties set the ensemble membership. The format is <b>server.<myid>=<hostname>:<leaderport>:<electionport></b>. Some explanation:</span></li>
<ul>
<li><span style="color: #444444;"><b>myid </b>is the server identification number. In this example, there are three servers, so each one will have a different myid with values 1, 2, and 3 respectively. The myid is set by creating a file named myid in the dataDir that contains a single integer in human readable ASCII text. This value must match one of the myid values from the configuration file. If another ensemble member has already been started with a conflicting myid value, an error will be thrown upon startup.</span></li>
<li><span style="color: #444444;"><b>leaderport </b>is used by followers to connect to the active leader. This port should be open between all ZooKeeper ensemble members.</span></li>
<li><span style="color: #444444;"><b>electionport </b>is used to perform leader elections between ensemble members. This port should be open between all ZooKeeper ensemble members.</span></li>
</ul>
</ul>
<div>
<span style="color: #444444;"><br /></span></div>
</div>
</div>
<span style="color: #6aa84f;"><span style="color: #444444;"></span> <span style="color: #444444; font-size: large;"><strong>KAFKA CLI</strong></span></span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">1. Start a zookeeper at default port 2181</span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
</div>
<div dir="ltr" style="text-align: left;">
<span style="color: #444444;"><span style="color: #3d85c6;">> bin/zookeeper-server-start.sh config/zookeeper.properties</span></span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">2. Start a kafka server at default port 9092</span><span style="color: #444444;"><br /></span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
</div>
<div dir="ltr" style="text-align: left;">
<span style="color: #3d85c6;">> bin/kafka-server-start.sh config/server.properties</span><br />
<br />
<span style="color: #444444;">3. Create a kafka topic with name my-first-topic</span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
<span style="color: #3d85c6;">> bin/kafka-topics.sh --zookeeper localhost:2181 --topic my-first-topic --create --replication-factor 1 --partitions 1</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">4. List all kafka topics</span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
<span style="color: #3d85c6;">> bin/kafka-topics.sh --zookeeper localhost:2181 --list</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">5. Describe kafka topic my-first-topic</span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
<span style="color: #3d85c6;">> bin/kafka-topics.sh --zookeeper localhost:2181 --topic my-first-topic --describe</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">6. Delete kafka topic my-first-topic</span><br />
<span style="color: #444444;"><br /></span></div>
<div dir="ltr" style="text-align: left;">
<span style="color: #3d85c6;">> bin/kafka-topics.sh --zookeeper localhost:2181 --topic my-first-topic --delete</span></div>
<div dir="ltr" style="text-align: left;">
<span style="color: #444444;">Note: This will have no impact if delete.topic.enable is not set to true</span><br />
<span style="color: #444444;"><br /></span>
<br />
<div dir="ltr" style="text-align: left;">
<span style="color: #444444;">7. Find out all the partitions without a leader</span><br />
<span style="color: #444444;"><br /></span></div>
<span style="color: #3d85c6;">> bin/kafka-topics.sh --zookeeper localhost:2181 --describe --unavailable-partitions</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">8. Produce messages to Kafka topic my-first-topic</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-first-topic --producer-property acks=all</span><br />
<span style="color: #3d85c6;">>hello ashish</span><br />
<span style="color: #3d85c6;">>learning kafka</span><br />
<span style="color: #3d85c6;">>^C</span><br />
<span style="color: #444444; font-family: "courier new" , "courier" , monospace; font-size: x-small;"><br /></span><span style="color: #444444;">9. Start Consuming messages from kafka topic my-first-topic</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-first-topic --from-beginning</span><br />
<span style="color: #3d85c6;">>hello ashish</span><br />
<span style="color: #3d85c6;">>learning kafka</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">10. Start Consuming messages in a consumer group from kafka topic my-first-topic</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-first-topic --group my-first-consumer-group --from-beginning</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">11. List all consumer groups</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">12. Describe consumer group</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe -group my-first-consumer-group</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">13. Reset offset of consumer group to replay all messages</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe -group my-first-consumer-group --reset-offsets --to-earliest --execute --topic my-first-topic</span><br />
<br />
<span style="color: #444444;">14. Shift offsets by 2 (forward) as another strategy</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-consumer-groups --bootstrap-server localhost:9092 --group my-first-consumer-group --reset-offsets --shift-by 2 --execute --topic my-first_topic</span><br />
<span style="color: #444444;"><br /></span> <span style="color: #444444;">15. Shift offsets by 2 (backward) as another strategy</span><br />
<span style="color: #444444;"><br /></span><span style="color: #3d85c6;"> > bin/kafka-consumer-groups --bootstrap-server localhost:9092 --group my-first-consumer-group --reset-offsets --shift-by -2 --execute --topic my-first_topic</span><br />
<br />
<br />
<div>
<div>
<ul style="text-align: left;"><ul>
</ul>
</ul>
</div>
<h3 style="text-align: left;">
<span style="color: #444444; font-size: large;">KAFKA API</span></h3>
</div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Click <a href="http://lahotisolutions.blogspot.com/2019/05/kafka-producer-using-java.html" target="_blank">here </a>to find out how we can create a Safe and high throughput Kafka Producer using Java.</span></li>
<li><span style="color: #444444;">Click <a href="https://lahotisolutions.blogspot.com/2019/05/kafka-consumer-using-java.html" target="_blank">here </a>to find out how we can create a Kafka consumer using Java with manual auto commit enabled.</span></li>
</ul>
<div>
<span style="color: #444444;"><br /></span></div>
<h2 style="text-align: left;">
<span style="color: #444444;">Default Ports</span></h2>
</div>
<div>
<ul style="text-align: left;">
<li><span style="color: #444444;">Zookeeper: 2181</span></li>
<li><span style="color: #444444;">Zookeeper Leader Port 3888</span></li>
<li><span style="color: #444444;">Zookeeper Election Port (Peer port) 2888</span></li>
<li><span style="color: #444444;">Broker: 9092</span></li>
<li><span style="color: #444444;">REST Proxy: 8082</span></li>
<li><span style="color: #444444;">Schema Registry: 8081</span></li>
<li><span style="color: #444444;">KSQL: 8088</span></li>
</ul>
</div>
</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com63tag:blogger.com,1999:blog-9017378041031770110.post-37508697328734898032018-05-01T23:06:00.000-07:002018-05-01T23:42:37.227-07:00Immutable Class Example<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background: #ffffff; border: 1px dashed #008def; color: black; font-size: 10pt; padding: 15px;">
<strong><span style="color: #7f0055;">package</span></strong> com.help4j.core;
<br />
<br />
<strong><span style="color: #7f0055;">import</span></strong> java.util.Date;
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Immutable class should mark as final so that it can not be extended by another </span><span style="color: #2040a0; font-size: 10pt;">class. </span><br />
<span style="color: #2040a0;"> * Fields should mark as private so direct access is not allowed
<br /> * Fields should mark as final so that value can not be modified once initialized
<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">final</span></strong> <strong><span style="color: #7f0055;">class</span></strong> MyImmutable {
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * String class is immutable<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">final</span></strong> String name;
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Integer class is immutable<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">final</span></strong> Integer weight;
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Date class is mutable<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">final</span></strong> Date dateOfBirth;
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Default private constructor will ensure no unplanned construction of class<br /> * All the final fields are initialized through constructor
<br /> */</span>
<br />
<strong><span style="color: #7f0055;">private</span></strong> MyImmutable(String name, Integer weight, Date dateOfBirth){
<br />
<strong><span style="color: #7f0055;">this</span></strong>.name = name;
<br />
<strong><span style="color: #7f0055;">this</span></strong>.weight = weight;
<br />
<strong><span style="color: #7f0055;">this</span></strong>.dateOfBirth = <strong><span style="color: #7f0055;">new</span></strong> Date(dateOfBirth.getTime());
<br />
}
<br />
<br />
/**********************************************
<br />
***********PROVIDE NO SETTER METHODS *********
<br />
**********************************************/
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * String class is immutable so we can return the instance variable as it is
<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">public</span></strong> String getName() {
<br />
<strong><span style="color: #7f0055;">return</span></strong> name;
<br />
}
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Integer class is immutable so we can return the instance variable as it is
<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">public</span></strong> Integer getWeight() {
<br />
<strong><span style="color: #7f0055;">return</span></strong> weight;
<br />
}
<br />
<br />
<span style="color: #2040a0;">/**
<br /> * Date class is mutable so we need a little care here.
<br /> * We should not return the reference of original instance variable.
<br /> * Instead a new Date object, with content copied to it, should be returned.
<br /> **/</span>
<br />
<strong><span style="color: #7f0055;">public</span></strong> Date getDateOfBirth() {
<br />
<strong><span style="color: #7f0055;">return</span></strong> <strong><span style="color: #7f0055;">new</span></strong> Date(dateOfBirth.getTime());
<br />
}<br />
}
<br />
<div style="border-top: #ccc 1px dashed; text-align: right;">
</div>
</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-8614161603249623092018-01-01T02:39:00.000-08:002019-09-29T17:35:10.889-07:00Top Java Interview Questions 2019 for experienced candidates<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #444444; font-family: "verdana" , sans-serif;">I have prepared this list from my personal experience of recent interviews. Topics are in the order of recent interview trend. Please follow this post on regular basis as i keep updating it.</span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Singleton Pattern</span></span></h2>
<div>
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span></div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">This is one of the most commonly asked question in interviews. Interviewer starts this question asking about design patterns. Candidate generally answers singleton and factory patterns. Then interviewer asks to write a singleton class. You should learn how to create a singleton class with private constructor, static getInstance method, double checked locking. You should also learn enum based singleton class. Follow up questions in singleton pattern are as follows:</span></span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">How to make singleton class thread safe?</span></span></li>
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">How to prevent deserialization to create new object of singleton class? </span></span></li>
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">How to prevent cloning and reflexion to create new object of singleton class?</span></span></li>
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">You can find the singleton pattern implementation here.</span></span></li>
</ul>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span></div>
<div style="text-align: left;">
<span style="color: #444444; font-family: "verdana" , sans-serif;">
</span></div>
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">HashMap</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">This is also most commonly asked question in collections. Interviewer start this question asking about collections and what are the data structure/collection you have used in development. Candidate generally answers HashMap, LinkedList, ArrayList, HashSet. Then interviewer check your knowledge of equals, hashcode and hashing algorithm. You should be aware of HashMap class internal implementation. Follow up questions in HashMap are as follows: </span></span><br />
<ul style="text-align: left;">
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">How to use HashMap in multi threading environment ? You should know that HashTable is thread safe. You can make HashMap thread safe by using Collections.synchronizedMap(Map) </span></span></li>
<li><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">What is concurrent hashmap ? How concurrent hashmap is better then thread safe hashmap in multi threading environment ?</span></span></li>
</ul>
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">LinkedList</span></span></h2>
<div>
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span></div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">LinkedList is something where interviewer can judge your knowledge of collections and algorithm at the same time. </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="font-family: "verdana" , sans-serif;">Collections: What is the difference in ArrayList and LinkedList ? </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">Algorithm: How to find middle element of Linked List? How to find a loop in LinkedList ? If you answer it then follow up question may asked:</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">How to find the starting point of loop in LinkedList ?</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">How to find the length of the loop in LinkedList ? </span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">MultiThreading</span></span></h2>
<div>
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span></div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">Interviewer can ask you any of following questions:</span></span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<span style="color: #444444;">
<span style="font-family: "verdana" , sans-serif;">1. How to run 5 threads sequentially.</span></span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">2. Print number 1 to 10 using two threads where thread 1 prints even number and thread 2 prints odd number.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">3. Producer consumer implementation using wait notify.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">4. What is deadlock ? How to identify deadlock in java application ? How to prevent deadlock situations in application development ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">5. CountDownLatch vs CyclicBarrier ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">6. Future Object, ThreadLocal etc.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">7. How to name a thread in executor service ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">8. How to interrupt a thread explicitly ?</span></span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">String</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">1. What is immutable object ? How can you write Immutable Class (<a href="http://lahotisolutions.blogspot.in/2018/05/immutable-class-example.html" target="_blank">See example</a>) ? If you know the internal implementation of String class and know how String is immutable, you can answer it easily. Here are few points to remember:-</span></span><br />
<br />
<ul style="text-align: left;">
<li><span style="color: #444444; font-family: "verdana" , sans-serif;">Declare the class as <b>final </b>so it can’t be extended.</span></li>
<li><span style="color: #444444; font-family: "verdana" , sans-serif;">Initialize all the fields via a constructor. Make all fields <b>private </b>so that direct access is not allowed. Make all fields <b>final </b>so that they’re initialized only once inside the constructor and never modified afterward.</span></li>
<li><span style="color: #444444; font-family: "verdana" , sans-serif;">Don’t provide setter methods.</span></li>
<li><span style="color: #444444; font-family: "verdana" , sans-serif;">When exposing methods which return mutable object then you must always return a new object with copied content of mutable object. Immutable variables can be returned safely without extra effort.</span></li>
</ul>
<br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">2. Difference between String object created using new and literal.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">3. Difference between StringBuilder and StringBuffer.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">4. Why character array is used over String to store password ? </span></span><br />
<br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Java Theory</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">1. S.O.L.I.D. (5 class design principle) in Java ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><br /></u></i></span></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><b>S</b>ingle Responsibility Principle</u></i> - One class should have one and only one responsibility</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><b>O</b>pen Close Principle</u></i> - Software components should be open for extension, but closed for modification</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><b>L</b>iskov Substitution</u></i> - Derived types must be completely substitutable for their base types</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><b>I</b>nterface Segregation </u></i>- Clients should not be forced to implement unnecessary methods which they will not use</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u><b>D</b>ependency Inversion</u></i> - Depend on abstractions, not on concretions</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">2. Anti patterns in Java. What is god class ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">3. How classes are related to each other through association, aggregation and composition.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u>Association</u></i> (bidirectional one to one, one to many, many to one or many to many association, represented by line with arrow in UML) for e.g. Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><u><i>Aggregation</i></u><i> (Has-a relationship, unidirectional association, parent and child can survive individually, represented by line with diamond in UML) for e.g. </i></span></span><span style="color: #444444; font-family: "verdana" , sans-serif;">Car and Wheel. Car can have multiple wheels but wheel can not belong to multiple cars and </span><span style="color: #444444; font-family: "verdana" , sans-serif;">if we delete the car, wheel need not to be destroyed and used in another car.</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><i><u>Composition</u></i> (Part-of relationship, unidirectional association, child can not survive without parent, </span></span><i style="color: #444444; font-family: verdana, sans-serif;">represented by line with filled diamond in UML</i><span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">) for e.g. </span></span><span style="color: #444444; font-family: "verdana" , sans-serif;">House and Rooms. </span><span style="color: #444444; font-family: "verdana" , sans-serif;">House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">4. Give example of 5-6 design patterns being used in Java classes. </span></span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<span style="color: #444444;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Factory:</u></i> java.util.Calendar#getInstance()</span></span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Abstract Factory:</u></i> javax.xml.parsers.DocumentBuilderFactory#newInstance(), javax.xml.transform.TransformerFactory#newInstance(), javax.xml.xpath.XPathFactory#newInstance()</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Builder:</u></i> java.lang.StringBuilder#append(), java.lang.StringBuffer#append()</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Decorator:</u></i> All subclasses of java.io.InputStream, OutputStream, Reader and Writer.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Chain of responsibility:</u></i> javax.servlet.Filter#doFilter()</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Iterator:</u></i> java.util.Iterator</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Observer:</u></i> JMS message listener</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Singleton:</u></i> java.lang.Runtime#getRuntime()</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"><i><u>Adapter:</u></i> java.util.Arrays#asList(), java.io.InputStreamReader(InputStream) (returns a Reader), java.io.OutputStreamWriter(OutputStream) (returns a Writer)</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span>
<br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Java Basics</span></span></h2>
<div>
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;"><br /></span></span></div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">1. Explain each keyword in main method i.e. public static void main(String[] args).</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"></span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">2. Can you override a static method and private method ?</span><br />
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span></div>
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Java Advance</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">1. How many ways you can create an object in java ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> Answer: New Keyword, Cloning, Deserialization and Reflection.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">2. Explain Java Memory Model with Heap structure. Explain here three parts of heap i.e. Young, Old and Permanent Generation. Also Explain Minor and Major GC</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">3. How to do JVM performance tuning ? Explain here parameters </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> a) Heap Memory: -Xms, -Xmx, -Xmn,</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> b) Permanent Generation Memory: -XX:PermSize, -XX:MaxPermSize</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> c) Garbage Collection i.e. </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> -XX:+UseSerialGC, </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> -XX:+UseParellelGC (-XX:ParellelGCThreads=<N>), </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> -XX:+UseParellelOldGC, </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> -XX:+UseConcMarkSweepGC (-XX:ParellelCMSThreads=<N>), </span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;"> -XX:+UseG1GC</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;"><br /></span><span style="font-family: "verdana" , sans-serif;">4. What is classloader in java ? How to write custom class Loader ? What is linkage error ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">5. Do you know about serialization ? When should we use that ? What is the role of Serialization id ? serializable vs externalizable ?</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><span style="font-family: "verdana" , sans-serif;">6. Deep vs Shallow copy in Cloning ?</span></span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Collection</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">These are legacy interview questions which are not asked frequently now a days but they are good to know.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">1. Difference between ArrayList and LinkedList.</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">2. Difference between ArrayList and Vector.</span></span><br />
<span style="font-family: "verdana" , sans-serif;"><span style="color: #444444;">
</span>
</span><br />
<h2 style="text-align: left;">
<span style="color: #444444;"><span style="font-family: "verdana" , sans-serif;">Application</span></span></h2>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;">
<span style="font-family: "verdana" , sans-serif;">1. Http vs https</span></span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;">
2. Rest vs soap</span><br />
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span>
<br />
<h2 style="text-align: left;">
<span style="color: #444444; font-family: "verdana" , sans-serif;">Microservices</span></h2>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">1. How do you manage distributed transaction across microservices ?</span></div>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">2. How do you collect logs and debug issues across microservices ?</span></div>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;">3. How do you manage user session across microservices ?</span></div>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span></div>
<div>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span></div>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #444444; font-family: "verdana" , sans-serif;"><br /></span></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-59527641467169830772017-07-22T06:15:00.001-07:002018-05-01T23:42:40.404-07:00Singleton Design Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<pre auto="" courier="" monospace="" new="" overflow:="" style="border: 1px dashed #008def; font-size: 13px; line-height: 18px; padding: 15px; text-align: left;"><span style="color: #7b0052; font-weight: bold;">package</span> com.help4j.advance.pattern;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.FileInputStream;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.FileNotFoundException;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.FileOutputStream;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.IOException;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.ObjectInputStream;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.ObjectOutputStream;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.ObjectStreamException;
<span style="color: #7b0052; font-weight: bold;">import</span> java.io.Serializable;
<span style="color: #3f5fbf;">/**
* @author aklahoti
*
*/</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">class</span> Singleton <span style="color: #7b0052; font-weight: bold;">implements</span> Serializable, Cloneable<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">void</span> main(String[] args) <span style="color: #7b0052; font-weight: bold;">throws</span> CloneNotSupportedException, FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #3f7f5f;">// Test creating an object of Singleton Class
</span> Singleton obj = Singleton.getInstance();
System.out.println(obj);
<span style="color: #3f7f5f;">// Test serialzation/deserialization of singleton object. It should not create new object and return same object.
</span> ObjectOutputStream os = <span style="color: #7b0052; font-weight: bold;">new</span> ObjectOutputStream(<span style="color: #7b0052; font-weight: bold;">new</span> FileOutputStream(<span style="color: #2a00ff;">"resources/singleton.ser"</span>));
os.writeObject(obj);
ObjectInputStream is = <span style="color: #7b0052; font-weight: bold;">new</span> ObjectInputStream(<span style="color: #7b0052; font-weight: bold;">new</span> FileInputStream(<span style="color: #2a00ff;">"resources/singleton.ser"</span>));
Singleton deserObj = (Singleton) is.readObject();
System.out.println(<span style="color: #2a00ff;">"***Deserialized Singleton Object***"</span>);
System.out.println(deserObj);
<span style="color: #3f7f5f;">// Test cloning of singleton object. It should not create new object and return same object.
</span> Singleton cloneObj = (Singleton) obj.clone();
System.out.println(<span style="color: #2a00ff;">"***Cloned Singleton Object***"</span>);
System.out.println(cloneObj);
<span style="color: #3f7f5f;">// Test reflexion of singleton object. It should throw exception.
</span> Class<Singleton> classObj = (Class<Singleton>) Class.forName(<span style="color: #2a00ff;">"com.java.test.pattern.Singleton"</span>);
System.out.println(classObj.newInstance());
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #7b0052; font-weight: bold;">private</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">final</span> <span style="color: #7b0052; font-weight: bold;">long</span> serialVersionUID = 1L;
<span style="color: #7b0052; font-weight: bold;">private</span> <span style="color: #7b0052; font-weight: bold;">static</span> Singleton instance = <span style="color: #7b0052; font-weight: bold;">null</span>;
<span style="color: #7b0052; font-weight: bold;">private</span> <span style="color: #7b0052; font-weight: bold;">static</span> Object DUMMY_OBJECT = <span style="color: #7b0052; font-weight: bold;">new</span> Object();
<span style="color: #7b0052; font-weight: bold;">private</span> Singleton()<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #3f7f5f;">/*To prevent object creation using reflection*/</span>
<span style="color: #7b0052; font-weight: bold;">if</span>(instance!=<span style="color: #7b0052; font-weight: bold;">null</span>)<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">throw</span> <span style="color: #7b0052; font-weight: bold;">new</span> InstantiationError( <span style="color: #2a00ff;">"Singleton Object is already created."</span> );
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> Singleton getInstance()<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #3f7f5f;">/*Double checked locking*/</span>
<span style="color: #7b0052; font-weight: bold;">if</span>(instance == <span style="color: #7b0052; font-weight: bold;">null</span>)<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">synchronized</span> (DUMMY_OBJECT) <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">if</span>(instance == <span style="color: #7b0052; font-weight: bold;">null</span>)<span style="color: #d3171b; font-weight: bold;">{</span>
instance = <span style="color: #7b0052; font-weight: bold;">new</span> Singleton();
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #7b0052; font-weight: bold;">return</span> instance;
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #7b0052; font-weight: bold;">public</span> <span style="color: #7b0052; font-weight: bold;">static</span> <span style="color: #7b0052; font-weight: bold;">void</span> print()<span style="color: #d3171b; font-weight: bold;">{</span>
System.out.println(<span style="color: #2a00ff;">"I am a singleton class."</span>);
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #3f7f5f;">/*To prevent object creation using deserialization*/</span>
<span style="color: #7b0052; font-weight: bold;">private</span> Object readResolve() <span style="color: #7b0052; font-weight: bold;">throws</span> ObjectStreamException<span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">return</span> instance;
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #3f7f5f;">/*To prevent object creation using cloning*/</span>
@Override
<span style="color: #7b0052; font-weight: bold;">protected</span> Object clone() <span style="color: #7b0052; font-weight: bold;">throws</span> CloneNotSupportedException <span style="color: #d3171b; font-weight: bold;">{</span>
<span style="color: #7b0052; font-weight: bold;">return</span> instance;
<span style="color: #d3171b; font-weight: bold;">}</span>
<span style="color: #d3171b; font-weight: bold;">}</span></pre>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-88257010584289233612017-03-26T01:17:00.001-07:002018-04-08T02:01:04.264-07:00Spring Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
<span style="background-color: white; font-family: "verdana" , sans-serif; font-size: x-small;">Spring Bean Life Cycle</span></h3>
<ol style="text-align: left;">
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> Default constructor will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> All properties setter methods will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements BeanNameAware then setBeanName method will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements BeanFactoryAware then setBeanFactory method will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements ApplicationContextAware then setApplicationContext method will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements BeanPostProcessor then its postProcessBeforeInitialization will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements InitializingBean then afterPropertiesSet method will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class has custom init method defined then it will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements BeanPostProcessor then its postProcessAfterInitializationwill be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class implements DisposableBean then destroy method will be called.</span></li>
<li><span style="font-family: "verdana" , sans-serif; font-size: x-small;"> If class has custom destroy method defined then it will be called. </span></li>
</ol>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b>Spring MVC flow</b></span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b><br /></b></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLqova5T7-woUASXwcjlG7Nxa9eFJyB-MY6DygJR0sBTUzV5qYXxvTceXckayO1kIj8_SfTT3jM0eD3Oby_FhjuWchNMWhyphenhyphenxqVF9Muu4cgZF14j_os7AaGht8AL5n2FVSuHv611VM7bBtw/s1600/Dispatcher-Servlet.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1062" data-original-width="1291" height="526" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLqova5T7-woUASXwcjlG7Nxa9eFJyB-MY6DygJR0sBTUzV5qYXxvTceXckayO1kIj8_SfTT3jM0eD3Oby_FhjuWchNMWhyphenhyphenxqVF9Muu4cgZF14j_os7AaGht8AL5n2FVSuHv611VM7bBtw/s640/Dispatcher-Servlet.png" width="640" /></a></div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b><br /></b></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">1. Dipatcher Servlet which is also know as front controller of Spring application handles all the request mapped to it in web.xml and ask for appropriate controller from Handler Mapping</span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">2. Handler Mapping is a configuration for url and controller mapping. It look for controller:</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><i>Popular Implentations of HandlerMapping</i>:</u></span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><br /></u></span>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b>BeanNameUrlHandlerMapping </b>- This is a default spring handler mapping. Name of bean considered as URL.</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"></span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"></span><br />
<pre><pre class=" language-markup" style="background: rgb(247, 247, 249); border-radius: 0px 0px 4px 4px; border: 1px solid rgb(209, 209, 232); box-sizing: border-box; font-family: consolas, monaco, "andale mono", "ubuntu mono", monospace; font-size: 13px; line-height: 1.5; margin-bottom: 20px; overflow: auto; padding: 1em; text-shadow: white 0px 1px; word-break: normal; word-wrap: normal;"><code class=" language-markup" style="background: none; border-radius: 0px; box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: inherit; hyphens: none; line-height: 1.5; padding: 0px; tab-size: 4; text-shadow: white 0px 1px; word-break: normal; word-spacing: normal; word-wrap: normal;"><span class="token tag" style="box-sizing: border-box;"><span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"><</span>bean</span><span style="color: #990055;"> </span><span class="token attr-name" style="box-sizing: border-box; color: #669900;">name</span><span class="token attr-value" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span><span style="color: #0077aa;">/welcome.htm</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span><span class="token punctuation" style="box-sizing: border-box;"><span style="color: #990055;"> </span></span></span><span class="token attr-name" style="box-sizing: border-box; color: #669900;">class</span><span class="token attr-value" style="box-sizing: border-box; color: #0077aa;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span>com.help4j.controller.WelcomeController<span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span></span><span style="color: #990055;"> </span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">/></span></span></code></pre>
</pre>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><br /></u></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b>SimpleUrlHandlerMapping </b>- Map with key value pair of URL and controller bean name.</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"></span><br />
<pre class=" language-markup" style="background: rgb(247, 247, 249); border-radius: 0px 0px 4px 4px; border: 1px solid rgb(209, 209, 232); box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: 13px; hyphens: none; line-height: 1.5; margin-bottom: 20px; overflow: auto; padding: 1em; tab-size: 4; text-shadow: white 0px 1px; word-break: normal; word-wrap: normal;"><code class=" language-markup" style="background: none; border-radius: 0px; box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: inherit; hyphens: none; line-height: 1.5; padding: 0px; tab-size: 4; text-shadow: white 0px 1px; word-break: normal; word-spacing: normal; word-wrap: normal;"><span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"><</span>bean</span> <span class="token attr-name" style="box-sizing: border-box; color: #669900;">class</span><span class="token attr-value" style="box-sizing: border-box; color: #0077aa;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span>org.springframework.web.servlet.handler.SimpleUrlHandlerMapping<span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span></span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"> <</span>property</span> <span class="token attr-name" style="box-sizing: border-box; color: #669900;">name</span><span class="token attr-value" style="box-sizing: border-box; color: #0077aa;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span>mappings<span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span></span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"><</span>props</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"><</span>prop</span> <span class="token attr-name" style="box-sizing: border-box; color: #669900;">key</span><span class="token attr-value" style="box-sizing: border-box; color: #0077aa;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span>/welcome.htm<span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span></span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>welcomeController<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"></</span>prop</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"></</span>props</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box;"> </span><span class="token punctuation" style="box-sizing: border-box; color: #999999;"></</span><span style="color: #990055;">property</span></span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token tag" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"></</span>bean</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">></span></span>
<span class="token tag" style="box-sizing: border-box;"><span class="token tag" style="box-sizing: border-box; color: #990055;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;"><</span>bean</span><span style="color: #990055;"> </span><span class="token attr-name" style="box-sizing: border-box; color: #669900;">id</span><span class="token attr-value" style="box-sizing: border-box;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span><span style="color: #0077aa;">welcomeController</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span><span class="token punctuation" style="box-sizing: border-box;"><span style="color: #990055;"> </span></span></span><span class="token attr-name" style="box-sizing: border-box; color: #669900;">class</span><span class="token attr-value" style="box-sizing: border-box; color: #0077aa;"><span class="token punctuation" style="box-sizing: border-box; color: #999999;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span>com.help4j.controller.WelcomeController<span class="token punctuation" style="box-sizing: border-box; color: #999999;">"</span></span><span style="color: #990055;"> </span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">/></span></span></code></pre>
</div>
<div>
<br />
<pre></pre>
</div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><b>DefaultAnnotationHandlerMapping </b>- Annotation based @RequestMapping at class and method level.</span><br />
<div class="filename" style="background-color: #e6e6fc; border-bottom: none; border-image: initial; border-left: 1px solid rgb(209, 209, 232); border-right: 1px solid rgb(209, 209, 232); border-top-left-radius: 4px; border-top-right-radius: 4px; border-top: 1px solid rgb(209, 209, 232); box-sizing: border-box; color: #333333; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 16px; margin-top: 20px; padding: 8px 8px 8px 14px;">
WelcomeController.java</div>
<pre class=" language-java" style="background: rgb(247, 247, 249); border-radius: 0px 0px 4px 4px; border: 1px solid rgb(209, 209, 232); box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: 13px; hyphens: none; line-height: 1.5; margin-bottom: 20px; overflow: auto; padding: 1em; tab-size: 4; text-shadow: white 0px 1px; word-break: normal; word-wrap: normal;"><code class=" language-java" style="background: none; border-radius: 0px; box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: inherit; hyphens: none; line-height: 1.5; padding: 0px; tab-size: 4; text-shadow: white 0px 1px; word-break: normal; word-spacing: normal; word-wrap: normal;"><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">package</span> com<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token punctuation" style="box-sizing: border-box;">help4j</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>controller<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">import</span> org<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>springframework<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>stereotype<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>Controller<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">import</span> org<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>springframework<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>web<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>bind<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>annotation<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>RequestMapping<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">import</span> org<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>springframework<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>web<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>bind<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>annotation<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>RequestMethod<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">import</span> org<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>springframework<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>web<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>servlet<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>ModelAndView<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token annotation punctuation" style="box-sizing: border-box; color: #999999;">@Controller</span>
<span class="token annotation punctuation" style="box-sizing: border-box; color: #999999;">@RequestMapping</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token string" style="box-sizing: border-box; color: #669900;">"/welcome"</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">public</span> <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">class</span> Welcome<span class="token class-name" style="box-sizing: border-box;">Controller</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
<span class="token annotation punctuation" style="box-sizing: border-box; color: #999999;">@RequestMapping</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>method <span class="token operator" style="background: rgba(255 , 255 , 255 , 0.498039); box-sizing: border-box; color: #a67f59;">=</span> RequestMethod<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span>GET<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">public</span> ModelAndView <span class="token function" style="box-sizing: border-box; color: #dd4a68;">helloWorld</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
ModelAndView model <span class="token operator" style="background: rgba(255 , 255 , 255 , 0.498039); box-sizing: border-box; color: #a67f59;">=</span> <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">new</span> <span class="token class-name" style="box-sizing: border-box;">ModelAndView</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token string" style="box-sizing: border-box; color: #669900;">"WelcomePage"</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
model<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">addObject</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token string" style="box-sizing: border-box; color: #669900;">"msg"</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> <span class="token string" style="box-sizing: border-box; color: #669900;">"hello world"</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: border-box; color: #0077aa;">return</span> model<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
<span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span>
<span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span></code></pre>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span>
<br />
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">3. Controller calls service layer to execute business logic and retrurn ModelAndView, which is wrapper for model object and view name.</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><i>Popular Controller Types:</i></u></span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><i><br /></i></u></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">AbstractController</span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">AbstractCommandController</span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="margin: 0px;">
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">SimpleFormController</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span></div>
</div>
</div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">4. View Resolver look for appropriate view JSP/HTML based on view name and return to user.</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><i>Popular Implentations of ViewResolver</i>:</u></span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><u><br /></u></span></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">InternalResourceViewResolver</span></div>
<div>
<br /></div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;"><br /></span></div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-47912410619065084822015-09-09T06:55:00.001-07:002015-09-09T06:55:10.547-07:00How to use superscript in HighChart labels<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">This is how you can use superscript in HighChart labels using useHTML property :</span><br />
<br />
(you can run and edit below code here : <a href="http://jsfiddle.net/lahoti32/j0ue1naf/">http://jsfiddle.net/lahoti32/j0ue1naf/</a> )<br />
<br />
<span style="background-color: white;">$(function () {</span><br />
<span style="background-color: white;"> $('#container').highcharts({</span><br />
<span style="background-color: white;"><span class="Apple-tab-span" style="white-space: pre;"> </span>chart: {</span><br />
<span style="background-color: white;"> type: 'column'</span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> credits : {</span><br />
<span style="background-color: white;"> enabled : false</span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> title: {</span><br />
<span style="background-color: white;"> text: 'Chart Title<sup>1</sup>',</span><br />
<span style="background-color: white;"> useHTML : true,</span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> xAxis: {</span><br />
<span style="background-color: white;"><span class="Apple-tab-span" style="white-space: pre;"> </span>categories: [ 'label1','label2','label3<sup>4</sup>','label4'],</span><br />
<span style="background-color: white;"> title:{</span><br />
<span style="background-color: white;"> enabled: false</span><br />
<span style="background-color: white;"> },<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> labels: {</span><br />
<span style="background-color: white;"> useHTML : true,</span><br />
<span style="background-color: white;"> title: {</span><br />
<span style="background-color: white;"> enabled: false</span><br />
<span style="background-color: white;"> } </span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> yAxis: {</span><br />
<span style="background-color: white;"> title: {</span><br />
<span style="background-color: white;"> enabled: false</span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> labels: {</span><br />
<span style="background-color: white;"> useHTML : true,</span><br />
<span style="background-color: white;"> formatter:function(){</span><br />
<span style="background-color: white;"> if(this.value != 10){</span><br />
<span style="background-color: white;"> return this.value;</span><br />
<span style="background-color: white;"> }else{</span><br />
<span style="background-color: white;"> return this.value + '<sup> 2</sup>';</span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> legend: {</span><br />
<span style="background-color: white;"> useHTML : true,</span><br />
<span style="background-color: white;"> borderWidth: 0,</span><br />
<span style="background-color: white;"> labelFormatter:function(){</span><br />
<span style="background-color: white;"> if(this.name != 'legend1'){</span><br />
<span style="background-color: white;"> return this.name;</span><br />
<span style="background-color: white;"> }else{</span><br />
<span style="background-color: white;"> return this.name + '<sup> 5</sup>';</span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> },</span><br />
<span style="background-color: white;"> plotOptions: {</span><br />
<span style="background-color: white;"> column: {</span><br />
<span style="background-color: white;"> dataLabels: {</span><br />
<span style="background-color: white;"> enabled: true,</span><br />
<span style="background-color: white;"> useHTML : true,</span><br />
<span style="background-color: white;"> y:-1,</span><br />
<span style="background-color: white;"> formatter:function(){</span><br />
<span style="background-color: white;"> if(this.y != 0){</span><br />
<span style="background-color: white;"> if(this.y > 8 && this.y < 10){</span><br />
<span style="background-color: white;"> return this.y + '<sup> 3</sup>';</span><br />
<span style="background-color: white;"> }else{</span><br />
<span style="background-color: white;"> return this.y;</span><br />
<span style="background-color: white;"> }</span><br />
<span style="background-color: white;"><br /></span>
<span style="background-color: white;"> }else{</span><br />
<span style="background-color: white;"> return null;</span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> },<span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="background-color: white;"> series: [{ </span><br />
<span style="background-color: white;"> data: [{ </span><br />
<span style="background-color: white;"> y: 14.913</span><br />
<span style="background-color: white;"> }, { </span><br />
<span style="background-color: white;"> y: 8.281</span><br />
<span style="background-color: white;"> }, { </span><br />
<span style="background-color: white;"> y: 3.592</span><br />
<span style="background-color: white;"> }, { </span><br />
<span style="background-color: white;"> y: 3.017</span><br />
<span style="background-color: white;"> }], </span><br />
<span style="background-color: white;"> showInLegend: false,</span><br />
<span style="background-color: white;"> },{</span><br />
<span style="background-color: white;"> name: 'legend1',</span><br />
<span style="background-color: white;"> color: 'rgb(14,178,89)' </span><br />
<span style="background-color: white;"> },{</span><br />
<span style="background-color: white;"> name: 'legend2',</span><br />
<span style="background-color: white;"> color: 'rgb(100,100,9)' </span><br />
<span style="background-color: white;"> }]</span><br />
<span style="background-color: white;"> });</span><br />
<span style="background-color: white;">});</span></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-39166778945517351022015-07-12T08:33:00.001-07:002015-07-12T08:33:45.481-07:00fibonacci series in java<div dir="ltr" style="text-align: left;" trbidi="on">
Fibonacci series implementation in java is frequently asked question in interview at fresher level. Moreover, it is a very famous example to show how to use recursive function in java.<br />
<br />
<br />
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: black; font-family: 'Courier New', Courier; font-size: 10pt; padding: 0px;">
<div style="background-color: white;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> Fibonacci {
</div>
<div style="background-color: white;">
<br /></div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args){
</div>
<div style="background-color: white;">
fibonacci(15);
</div>
<div style="background-color: white;">
}
</div>
<div style="background-color: white;">
</div>
<div style="background-color: white;">
<span style="color: #2040a0;">/**
<br /> * This method is used to print fibonacci series
<br /> * <strong>@param</strong> n is total number of elements
<br /> */</span>
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> fibonacci(<strong><span style="color: #7f0055;">int</span></strong> n){
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">for</span></strong>(<strong><span style="color: #7f0055;">int</span></strong> i = 0 ; i < n; i ++){
</div>
<div style="background-color: white;">
System.out.print(fib(i)+<span style="color: #2a00ff;">", "</span>);
</div>
<div style="background-color: white;">
}
</div>
<div style="background-color: white;">
}
</div>
<div style="background-color: white;">
<span style="color: #2040a0;">/**
<br /> * This method is used recursively to find fibonacci
<br /> * element at nth index.
<br /> * <strong>@param</strong> n is index
<br /> * <strong>@return</strong> element<br /> */</span>
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">int</span></strong> fib(<strong><span style="color: #7f0055;">int</span></strong> n){
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">if</span></strong>(n < 2)
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">return</span></strong> n;
</div>
<div style="background-color: white;">
<strong><span style="color: #7f0055;">return</span></strong> fib(n-1) + fib(n-2);
</div>
<div style="background-color: white;">
}
</div>
<div style="background-color: white;">
}</div>
<div style="background-color: white;">
<br /></div>
<b style="background-color: yellow;">Output :</b><span style="background-color: white;"> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 </span><br />
<div style="background-color: white; border-top-color: rgb(204, 204, 204); border-top-style: dashed; border-top-width: 1px; text-align: right;">
</div>
</div>
<br /></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-21374565600722684292015-06-09T03:04:00.002-07:002015-06-09T03:05:22.582-07:00Sorting Algorithm : Bubble Sort<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<b><span style="color: #38761d;">Why it is called bubble sort ?</span></b></h2>
<div style="text-align: left;">
Bubble Sort is nothing but a comparison algorithm where - </div>
<div style="text-align: left;">
- At the end of first iteration, largest element in the array get placed at last index</div>
<div style="text-align: left;">
- At the end of second iteration, second largest element in the array get placed at second last index and so on...</div>
<div style="text-align: left;">
This way large elements are moving towards the last indexes and hence small elements are moving towards the starting indexes which is also termed as smaller elements "bubble" to the top of the list that is why it is called bubble sort.</div>
<h3 style="background: none rgb(255, 255, 255); border-bottom-style: none; font-family: sans-serif; font-size: 1.2em; line-height: 1.6; margin: 0.3em 0px 0px; overflow: hidden; padding-bottom: 0px; padding-top: 0.5em;">
<span class="mw-headline" id="Step-by-step_example"><span style="color: #38761d;">Step-by-step example</span></span></h3>
<div style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; margin-bottom: 0.5em; margin-top: 0.5em;">
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in <b>bold</b> are being compared. Three passes will be required.</div>
<div style="font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; margin-bottom: 0.5em; margin-top: 0.5em;">
<div style="background-color: white; color: #252525;">
<b>First Pass:</b></div>
<div style="background-color: white; color: #252525;">
( <b>5</b> <b>1</b> 4 2 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( <b>1</b> <b>5</b> 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.</div>
<div style="background-color: white; color: #252525;">
( 1 <b>5</b> <b>4</b> 2 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 <b>4</b> <b>5</b> 2 8 ), Swap since 5 > 4</div>
<div style="background-color: white; color: #252525;">
( 1 4 <b>5</b> <b>2</b> 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 4 <b>2</b> <b>5</b> 8 ), Swap since 5 > 2</div>
<div style="background-color: white; color: #252525;">
( 1 4 2 <b>5</b> <b>8</b> ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 4 2 <b>5</b> <b>8</b> ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.</div>
<div style="background-color: white; color: #252525;">
<b>Second Pass:</b></div>
<div style="background-color: white; color: #252525;">
( <b>1</b> <b>4</b> 2 5 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( <b>1</b> <b>4</b> 2 5 8 )</div>
<div style="background-color: white; color: #252525;">
( 1 <b>4</b> <b>2</b> 5 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 <b>2</b> <b>4</b> 5 8 ), Swap since 4 > 2</div>
<div style="background-color: white; color: #252525;">
( 1 2 <b>4</b> <b>5</b> 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 2 <b>4</b> <b>5</b> 8 )</div>
<div style="background-color: white; color: #252525;">
( 1 2 4 <b>5</b> <b>8</b> ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 2 4 <b>5</b> <b>8</b> )</div>
<div style="background-color: white; color: #252525;">
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one <b>whole</b> pass without <b>any</b> swap to know it is sorted.</div>
<div style="background-color: white; color: #252525;">
<b>Third Pass:</b></div>
<div style="background-color: white; color: #252525;">
( <b>1</b> <b>2</b> 4 5 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( <b>1</b> <b>2</b> 4 5 8 )</div>
<div style="background-color: white; color: #252525;">
( 1 <b>2</b> <b>4</b> 5 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 <b>2</b> <b>4</b> 5 8 )</div>
<div style="background-color: white; color: #252525;">
( 1 2 <b>4</b> <b>5</b> 8 ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 2 <b>4</b> <b>5</b> 8 )</div>
<div style="background-color: white; color: #252525;">
( 1 2 4 <b>5</b> <b>8</b> ) <img alt="\to" class="mwe-math-fallback-image-inline tex" src="http://upload.wikimedia.org/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border: none; display: inline-block; margin: 0px; vertical-align: middle;" /> ( 1 2 4 <b>5</b> <b>8</b> )</div>
<div style="background-color: white; color: #252525;">
<br /></div>
<h2 style="text-align: left;">
<span style="background-color: white;"><span style="color: #38761d;">Algorithm In Java</span></span></h2>
<div style="background-color: white; color: #252525;">
<br /></div>
<div style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: black; font-family: 'Courier New', Courier; font-size: 10pt; padding: 0px;">
<strong><span style="color: #7f0055;">package</span></strong> com.test.algorithm.sort;
<br />
<span style="color: #3f7f5f;">/*
<br /> *Bubble Sort is nothing but a comparison algorithm where -
<br /> *- At the end of first iteration, largest element in the array get placed at last index
<br /> *- At the end of second iteration, second largest element in the array get placed at second last index and so on...
<br /> *This way large elements are moving towards the last indexes and hence small elements are moving towards the starting indexes
<br /> *which is also termed as smaller elements "bubble" to the top of the list that is why it is called bubble sort.
<br /> */</span>
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> BubbleSort {
<br />
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args){
<br />
<strong><span style="color: #7f0055;">int</span></strong>[] array = <strong><span style="color: #7f0055;">new</span></strong> <strong><span style="color: #7f0055;">int</span></strong>[]{5, 1, 12, -5, 16};
<br />
BubbleSort.sort(array);
<br />
}
<br />
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> sort(<strong><span style="color: #7f0055;">int</span></strong>[] array){
<br />
<strong><span style="color: #7f0055;">int</span></strong> count = 0;
<br />
<strong><span style="color: #7f0055;">for</span></strong>(<strong><span style="color: #7f0055;">int</span></strong> i = array.length ; i > 0 ; i --, count++){
<br />
<strong><span style="color: #7f0055;">for</span></strong>(<strong><span style="color: #7f0055;">int</span></strong> j = 0 ; j < array.length-1 ; j++, count++){
<br />
<strong><span style="color: #7f0055;">if</span></strong>(array[j] > array[j+1]){
<br />
swapNumbers(j, j+1, array);
<br />
}
<br />
}
<br />
}
<br />
printNumbers(array, count);
<br />
}
<br />
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> swapNumbers(<strong><span style="color: #7f0055;">int</span></strong> i, <strong><span style="color: #7f0055;">int</span></strong> j, <strong><span style="color: #7f0055;">int</span></strong>[] array) {
<br />
<strong><span style="color: #7f0055;">int</span></strong> temp;
<br />
temp = array[i];
<br />
array[i] = array[j];
<br />
array[j] = temp;
<br />
}
<br />
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> printNumbers(<strong><span style="color: #7f0055;">int</span></strong>[] array, <strong><span style="color: #7f0055;">int</span></strong> count) {
<br />
System.out.print(<span style="color: #2a00ff;">"Sorted Array : {"</span>);
<br />
<strong><span style="color: #7f0055;">for</span></strong>(<strong><span style="color: #7f0055;">int</span></strong> i : array){
<br />
System.out.print(i + <span style="color: #2a00ff;">" "</span>);
<br />
}
<br />
System.out.print(<span style="color: #2a00ff;">"}, n : "</span> + array.length + <span style="color: #2a00ff;">", comparisons : "</span> + count);
<br />
}
<br />
}
<br />
<div style="border-top: #ccc 1px dashed; text-align: right;">
</div>
</div>
<div style="background-color: white;">
<div style="color: #252525;">
<br /></div>
<b style="color: #252525;">Output :</b><span style="color: #252525;"> </span><span style="color: #38761d;"><b>Sorted Array : {-5 1 5 12 16 }, n : 5, comparisons : 25</b></span><br />
<span style="color: #38761d;"><b><br /></b></span>
Time Complexity for worst case is O(n2). Algorithm can be further improved :<br />
<span style="color: #38761d;"><b><br /></b></span>
</div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: 'Courier New', Courier; font-size: 10pt; padding: 0px;">
<div style="background-color: white; color: black;">
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> sort(<strong><span style="color: #7f0055;">int</span></strong>[] array){
</div>
<div style="background-color: white; color: black;">
<strong><span style="color: #7f0055;">int</span></strong> count = 0;
</div>
<div style="background-color: white; color: black;">
<strong><span style="color: #7f0055;">for</span></strong>(<strong><span style="color: #7f0055;">int</span></strong> i = array.length ; i > 0 ; i --, count++){
</div>
<span style="background-color: white; color: black;"> </span><strong style="background-color: white; color: black;"><span style="color: #7f0055;">for</span></strong><span style="background-color: white; color: black;">(</span><strong style="background-color: white; color: black;"><span style="color: #7f0055;">int</span></strong><span style="background-color: white; color: black;"> j = 0 ; j < </span><b style="background-color: yellow;">i-1</b><span style="background-color: white; color: black;"> ; j++, count++){
</span><br />
<div style="background-color: white; color: black;">
<strong><span style="color: #7f0055;">if</span></strong>(array[j] > array[j+1]){
</div>
<div style="background-color: white; color: black;">
swapNumbers(j, j+1, array);
</div>
<div style="background-color: white; color: black;">
}
</div>
<div style="background-color: white; color: black;">
}
</div>
<div style="background-color: white; color: black;">
}
</div>
<div style="background-color: white; color: black;">
printNumbers(array, count);
</div>
<div style="background-color: white; color: black;">
}<br />
<br /></div>
<div style="background-color: white; border-top-color: rgb(204, 204, 204); border-top-style: dashed; border-top-width: 1px; color: black; text-align: left;">
<b style="color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; text-align: left;">Output :</b><span style="color: #38761d; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; text-align: left;"><b>Sorted Array : {-5 1 5 12 16 }, n : 5, comparisons : 15</b></span></div>
</div>
</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-25271582447130312812015-05-01T02:38:00.000-07:002015-05-01T02:38:02.877-07:00Stack Implementation Using Priority Queue<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; border: 0px; clear: both; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 19.5px; margin-bottom: 1em; padding: 0px;">
<span style="border: 0px; margin: 0px; padding: 0px;"><i>Stacks may be modeled as particular kinds of priority queues. In a stack, the priority of each inserted element is monotonically increasing; thus, the last element inserted is always the first retrieved.</i></span></div>
<em style="border: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 19.5px; margin: 0px; padding: 0px;"><u><b><span style="color: #7f6000;">Stack Implementation : </span></b></u></em><br />
<br />
<div style="background: rgb(255, 255, 255); padding: 0px;">
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> java.util.NoSuchElementException;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">import</span></strong> java.util.PriorityQueue;
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> StackImplementationUsingPQ {
</div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<br /></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<strong><span style="color: #7f0055;">private</span></strong> PriorityQueue<element> pq = <strong><span style="color: #7f0055;">new</span></strong> PriorityQueue<element>();
<br /> <strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">int</span></strong> counter = 0;
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> push(String s){
<br /> Element e = <strong><span style="color: #7f0055;">new</span></strong> Element(s, counter++);
<br /> pq.add(e);
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> String pop(){
<br /> Element e = <strong><span style="color: #7f0055;">null</span></strong>;
<br /> <strong><span style="color: #7f0055;">try</span></strong>{
<br /> e = pq.remove();
<br /> counter--;
<br /> }<strong><span style="color: #7f0055;">catch</span></strong>(NoSuchElementException ex){
<br /> <strong><span style="color: #7f0055;">return</span></strong> <span style="color: #2a00ff;">"'Stack is Empty'"</span>;
<br /> }
<br /> <strong><span style="color: #7f0055;">return</span></strong> e.toString();
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args){
<br /> StackImplementationUsingPQ stack = <strong><span style="color: #7f0055;">new</span></strong> StackImplementationUsingPQ();
<br /> stack.push(<span style="color: #2a00ff;">"Banana"</span>);
<br /> stack.push(<span style="color: #2a00ff;">"Mango"</span>);
<br /> stack.push(<span style="color: #2a00ff;">"Apple"</span>);
<br /> System.out.println(stack.pop());
<br /> stack.push(<span style="color: #2a00ff;">"Orange"</span>);
<br /> stack.push(<span style="color: #2a00ff;">"Pinapple"</span>);
<br /> System.out.println(stack.pop());
<br /> System.out.println(stack.pop());
<br /> System.out.println(stack.pop());
<br /> System.out.println(stack.pop());
<br /> System.out.println(stack.pop());
<br /> }
<br />
<br />}
<br /><strong><span style="color: #7f0055;">class</span></strong> Element <strong><span style="color: #7f0055;">implements</span></strong> Comparable<element>{
<br />
<br /> <strong><span style="color: #7f0055;">private</span></strong> Integer priority;
<br /> <strong><span style="color: #7f0055;">private</span></strong> String value;
<br />
<br /> Element(String value, Integer priority){
<br /> <strong><span style="color: #7f0055;">this</span></strong>.value = value;
<br /> <strong><span style="color: #7f0055;">this</span></strong>.priority = priority;
<br /> }
<br />
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> Integer getPriority() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> priority;
<br /> }
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setPriority(Integer priority) {
<br /> <strong><span style="color: #7f0055;">this</span></strong>.priority = priority;
<br /> }
<br /> <strong><span style="color: #7f0055;">public</span></strong> String getValue() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> value;
<br /> }
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setValue(String value) {
<br /> <strong><span style="color: #7f0055;">this</span></strong>.value = value;
<br /> }
<br />
<br /> <span style="color: #646464;">@Override</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">int</span></strong> compareTo(Element o) {
<br /> <strong><span style="color: #7f0055;">return</span></strong> o.getPriority().compareTo(<strong><span style="color: #7f0055;">this</span></strong>.getPriority());
<br /> }
<br />
<br /> <span style="color: #646464;">@Override</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> String toString() {
<br /> StringBuilder sb = <strong><span style="color: #7f0055;">new</span></strong> StringBuilder();
<br /> sb.append(<span style="color: #2a00ff;">"value : "</span>);
<br /> sb.append(getValue());
<br /> sb.append(<span style="color: #2a00ff;">", "</span>);
<br /> sb.append(<span style="color: #2a00ff;">"priority : "</span>);
<br /> sb.append(getPriority());
<br /> <strong><span style="color: #7f0055;">return</span></strong> sb.toString();
<br /> }
<br />} </element></element></element></div>
<div style="color: black; font-family: 'Courier New', Courier; font-size: 10pt;">
<element><element><element><br /></element></element></element></div>
<element><element><element><span style="color: #7f6000; font-family: Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 15px; line-height: 19.5px;"><i><b><u>Output:</u></b></i></span></span></element></element></element><br />
<element><element><element><span style="color: #222222; font-family: Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 15px; line-height: 19.5px;"><i><u><br /></u></i></span></span></element></element></element>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">value : Apple, priority : 2</span></span></div>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">value : Pinapple, priority : 3</span></span></div>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">value : Orange, priority : 2</span></span></div>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">value : Mango, priority : 1</span></span></div>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">value : Banana, priority : 0</span></span></div>
<div style="text-align: left;">
<span style="color: #222222; font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="line-height: 19.5px;">'Stack is Empty'</span></span></div>
<div>
<br /></div>
<element><element><element><span style="color: #222222; font-family: Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 15px; line-height: 19.5px;"><i><u></u></i></span></span>
</element></element></element></div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-92177271143083204462015-04-06T23:56:00.000-07:002015-05-01T02:12:00.294-07:00Elevator Implementation in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Implementation of Elevator/ Lift has been asked in many interviews. I have tried to implement it using muti-threading and TreeSet. TreeSet is used to store incoming request. It is a good choice here as it removes the duplicate requests and implements NavigableSet which provide you methods such as floor and ceiling.<br />
<br />
Elevator in this program implements following features -<br />
<ul style="text-align: left;">
<li>If elevator is going up or down, it checks for nearest floor request to process first in that direction. </li>
<li>If there is no request to process, it waits at last processed floor.</li>
<li>If a new request comes while elevator is processing a request. It process the new request first if it is nearest than the processing floor in same direction.</li>
</ul>
<br />
<br />
<br />
<br />
<div style="background: #FFFFFF; color: black; font-family: Courier New, Courier; font-size: 10pt; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<strong><span style="color: #7f0055;">import</span></strong> java.io.BufferedReader;
<br />
<strong><span style="color: #7f0055;">import</span></strong> java.io.IOException;
<br />
<strong><span style="color: #7f0055;">import</span></strong> java.io.InputStreamReader;
<br />
<strong><span style="color: #7f0055;">import</span></strong> java.util.TreeSet;
<br />
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> MyLift {
<br />
<br />
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">static</span></strong> <strong><span style="color: #7f0055;">void</span></strong> main(String[] args) {
<br />
System.out.println(<span style="color: #2a00ff;">"Welcome to MyLift"</span>);
<br />
<span style="color: #3f7f5f;">// RequestListenerThread to read requested floor and add to Set
</span><br />
Thread requestListenerThread = <strong><span style="color: #7f0055;">new</span></strong> Thread(<strong><span style="color: #7f0055;">new</span></strong> RequestListener(),
<br />
<span style="color: #2a00ff;">"RequestListenerThread"</span>);
<br />
<span style="color: #3f7f5f;">// RequestProcessorThread to read Set and process requested floor
</span><br />
Thread requestProcessorThread = <strong><span style="color: #7f0055;">new</span></strong> Thread(<strong><span style="color: #7f0055;">new</span></strong> RequestProcessor(),
<br />
<span style="color: #2a00ff;">"RequestProcessorThread"</span>);
<br />
<br />
Elevator.getInstance().setRequestProcessorThread(requestProcessorThread);
<br />
<br />
requestListenerThread.start();
<br />
requestProcessorThread.start();
<br />
<br />
<br />
}
<br />
}
<br />
<br />
<strong><span style="color: #7f0055;">class</span></strong> Elevator {
<br />
<br />
<strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">static</span></strong> Elevator elevator = <strong><span style="color: #7f0055;">null</span></strong>;
<br />
<br />
<strong><span style="color: #7f0055;">private</span></strong> TreeSet<integer> requestSet = <strong><span style="color: #7f0055;">new</span></strong> TreeSet<integer>();
<br />
<br /> <strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">int</span></strong> currentFloor = 0;
<br />
<br /> <strong><span style="color: #7f0055;">private</span></strong> Direction direction = Direction.UP;
<br />
<br /> <strong><span style="color: #7f0055;">private</span></strong> Elevator() {};
<br />
<br /> <strong><span style="color: #7f0055;">private</span></strong> Thread requestProcessorThread;
<br />
<br /> <span style="color: #2040a0;">/**
<br /> * <strong>@return</strong> singleton instance
<br /> */</span>
<br /> <strong><span style="color: #7f0055;">static</span></strong> Elevator getInstance() {
<br /> <strong><span style="color: #7f0055;">if</span></strong> (elevator == <strong><span style="color: #7f0055;">null</span></strong>) {
<br /> elevator = <strong><span style="color: #7f0055;">new</span></strong> Elevator();
<br /> }
<br /> <strong><span style="color: #7f0055;">return</span></strong> elevator;
<br /> }
<br />
<br /> <span style="color: #2040a0;">/**
<br /> * Add request to Set
<br /> *
<br /> * <strong>@param</strong> floor
<br /> */</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">synchronized</span></strong> <strong><span style="color: #7f0055;">void</span></strong> addFloor(<strong><span style="color: #7f0055;">int</span></strong> f) {
<br /> requestSet.add(f);
<br />
<br /> <strong><span style="color: #7f0055;">if</span></strong>(requestProcessorThread.getState() == Thread.State.WAITING){
<br /> <span style="color: #3f7f5f;">// Notify processor thread that a new request has come if it is waiting
</span><br /> notify();
<br /> }<strong><span style="color: #7f0055;">else</span></strong>{
<br /> <span style="color: #3f7f5f;">// Interrupt Processor thread to check if new request should be processed before current request or not.
</span><br /> requestProcessorThread.interrupt();
<br /> }
<br />
<br /> }
<br />
<br /> <span style="color: #2040a0;">/**
<br /> * <strong>@return</strong> next request to process based on elevator current floor and direction
<br /> */</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">synchronized</span></strong> <strong><span style="color: #7f0055;">int</span></strong> nextFloor() {
<br />
<br /> Integer floor = <strong><span style="color: #7f0055;">null</span></strong>;
<br />
<br /> <strong><span style="color: #7f0055;">if</span></strong> (direction == Direction.UP) {
<br /> <strong><span style="color: #7f0055;">if</span></strong> (requestSet.ceiling(currentFloor) != <strong><span style="color: #7f0055;">null</span></strong>) {
<br /> floor = requestSet.ceiling(currentFloor);
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> floor = requestSet.floor(currentFloor);
<br /> }
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> <strong><span style="color: #7f0055;">if</span></strong> (requestSet.floor(currentFloor) != <strong><span style="color: #7f0055;">null</span></strong>) {
<br /> floor = requestSet.floor(currentFloor);
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> floor = requestSet.ceiling(currentFloor);
<br /> }
<br /> }
<br /> <strong><span style="color: #7f0055;">if</span></strong> (floor == <strong><span style="color: #7f0055;">null</span></strong>) {
<br /> <strong><span style="color: #7f0055;">try</span></strong> {
<br /> System.out.println(<span style="color: #2a00ff;">"Waiting at Floor :"</span> + getCurrentFloor());
<br /> wait();
<br /> } <strong><span style="color: #7f0055;">catch</span></strong> (InterruptedException e) {
<br /> e.printStackTrace();
<br /> }
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> <span style="color: #3f7f5f;">// Remove the request from Set as it is the request in Progress.
</span><br /> requestSet.remove(floor);
<br /> }
<br /> <strong><span style="color: #7f0055;">return</span></strong> (floor == <strong><span style="color: #7f0055;">null</span></strong>) ? -1 : floor;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">int</span></strong> getCurrentFloor() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> currentFloor;
<br /> }
<br />
<br /> <span style="color: #2040a0;">/**
<br /> * Set current floor and direction based on requested floor
<br /> *
<br /> * <strong>@param</strong> currentFloor
<br /> * <strong>@throws</strong> InterruptedException
<br /> */</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setCurrentFloor(<strong><span style="color: #7f0055;">int</span></strong> currentFloor) <strong><span style="color: #7f0055;">throws</span></strong> InterruptedException {
<br /> <strong><span style="color: #7f0055;">if</span></strong> (<strong><span style="color: #7f0055;">this</span></strong>.currentFloor > currentFloor) {
<br /> setDirection(Direction.DOWN);
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> setDirection(Direction.UP);
<br /> }
<br /> <strong><span style="color: #7f0055;">this</span></strong>.currentFloor = currentFloor;
<br />
<br /> System.out.println(<span style="color: #2a00ff;">"Floor : "</span> + currentFloor);
<br />
<br /> Thread.sleep(3000);
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> Direction getDirection() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> direction;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setDirection(Direction direction) {
<br /> <strong><span style="color: #7f0055;">this</span></strong>.direction = direction;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> Thread getRequestProcessorThread() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> requestProcessorThread;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setRequestProcessorThread(Thread requestProcessorThread) {
<br /> <strong><span style="color: #7f0055;">this</span></strong>.requestProcessorThread = requestProcessorThread;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> TreeSet<integer> getRequestSet() {
<br /> <strong><span style="color: #7f0055;">return</span></strong> requestSet;
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> setRequestSet(TreeSet<integer> requestSet) {
<br /> <strong><span style="color: #7f0055;">this</span></strong>.requestSet = requestSet;
<br /> }
<br />
<br />}
<br />
<br /><strong><span style="color: #7f0055;">class</span></strong> RequestProcessor <strong><span style="color: #7f0055;">implements</span></strong> Runnable {
<br />
<br /> <span style="color: #646464;">@Override</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> run() {
<br /> <strong><span style="color: #7f0055;">while</span></strong> (<strong><span style="color: #7f0055;">true</span></strong>) {
<br /> Elevator elevator = Elevator.getInstance();
<br /> <strong><span style="color: #7f0055;">int</span></strong> floor = elevator.nextFloor();
<br /> <strong><span style="color: #7f0055;">int</span></strong> currentFloor = elevator.getCurrentFloor();
<br /> <strong><span style="color: #7f0055;">try</span></strong>{
<br /> <strong><span style="color: #7f0055;">if</span></strong> (floor >= 0) {
<br /> <strong><span style="color: #7f0055;">if</span></strong> (currentFloor > floor) {
<br /> <strong><span style="color: #7f0055;">while</span></strong> (currentFloor > floor) {
<br /> elevator.setCurrentFloor(--currentFloor);
<br /> }
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> <strong><span style="color: #7f0055;">while</span></strong> (currentFloor < floor) {
<br /> elevator.setCurrentFloor(++currentFloor);
<br /> }
<br /> }
<br /> System.out.println(<span style="color: #2a00ff;">"Welcome to Floor : "</span> + elevator.getCurrentFloor());
<br /> }
<br />
<br /> }<strong><span style="color: #7f0055;">catch</span></strong>(InterruptedException e){
<br /> <span style="color: #3f7f5f;">// If a new request has interrupted a current request processing then check -
</span><br /> <span style="color: #3f7f5f;">// -if the current request is already processed
</span><br /> <span style="color: #3f7f5f;">// -otherwise add it back in request Set
</span><br /> <strong><span style="color: #7f0055;">if</span></strong>(elevator.getCurrentFloor() != floor){
<br /> elevator.getRequestSet().add(floor);
<br /> }
<br /> }
<br /> }
<br /> }
<br />}
<br />
<br /><strong><span style="color: #7f0055;">class</span></strong> RequestListener <strong><span style="color: #7f0055;">implements</span></strong> Runnable {
<br />
<br /> <span style="color: #646464;">@Override</span>
<br /> <strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">void</span></strong> run() {
<br />
<br /> <strong><span style="color: #7f0055;">while</span></strong> (<strong><span style="color: #7f0055;">true</span></strong>) {
<br /> String floorNumberStr = <strong><span style="color: #7f0055;">null</span></strong>;
<br /> <strong><span style="color: #7f0055;">try</span></strong> {
<br /> <span style="color: #3f7f5f;">// Read input from console
</span><br /> BufferedReader bufferedReader = <strong><span style="color: #7f0055;">new</span></strong> BufferedReader(<strong><span style="color: #7f0055;">new</span></strong> InputStreamReader(System.in));
<br /> floorNumberStr = bufferedReader.readLine();
<br /> } <strong><span style="color: #7f0055;">catch</span></strong> (IOException e) {
<br /> e.printStackTrace();
<br /> }
<br />
<br /> <strong><span style="color: #7f0055;">if</span></strong> (isValidFloorNumber(floorNumberStr)) {
<br /> System.out.println(<span style="color: #2a00ff;">"User Pressed : "</span> + floorNumberStr);
<br /> Elevator elevator = Elevator.getInstance();
<br /> elevator.addFloor(Integer.parseInt(floorNumberStr));
<br /> } <strong><span style="color: #7f0055;">else</span></strong> {
<br /> System.out.println(<span style="color: #2a00ff;">"Floor Request Invalid : "</span> + floorNumberStr);
<br /> }
<br /> }
<br /> }
<br />
<br /> <span style="color: #2040a0;">/**
<br /> * This method is used to define maximum floors this elevator can process.
<br /> * <strong>@param</strong> s - requested floor
<br /> * <strong>@return</strong> true if requested floor is integer and upto two digits. (max floor = 99)
<br /> */</span>
<br /> <strong><span style="color: #7f0055;">private</span></strong> <strong><span style="color: #7f0055;">boolean</span></strong> isValidFloorNumber(String s) {
<br /> <strong><span style="color: #7f0055;">return</span></strong> (s != <strong><span style="color: #7f0055;">null</span></strong>) && s.matches(<span style="color: #2a00ff;">"\\d{1,2}"</span>);
<br /> }
<br />
<br />}
<br />
<br /><strong><span style="color: #7f0055;">enum</span></strong> Direction {
<br /> UP, DOWN
<br />}
</integer></integer></integer></integer></div>
</div>
<br /></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com2tag:blogger.com,1999:blog-9017378041031770110.post-39445051598111414532013-06-05T05:19:00.001-07:002013-06-05T05:19:26.371-07:00Check for Updates in eclipse is not working<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<ol style="background-color: white; border: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1em 30px; padding: 0px; vertical-align: baseline;">
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">In Eclipse (go to Preferences > General > Network connections).</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">Select "Active Provider" as "Manual".</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">Select HTTP and click edit.</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">Enter the host and port</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">Select "Requires Authentication" and enter the username and password.</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Repeat Step 1 to 5 for Https.
</li>
<li style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
In "proxy bypass" section click "Add host"
</li>
</ol>
<div>
In case it does not work try to play with Active Provider - General , Manual , Native</div>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-52348639589193429312013-05-22T23:29:00.000-07:002013-05-22T23:29:09.649-07:00How To Run PHP Project<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="background-color: #fafafa; color: #152a3d; font-family: 'Lucida Grande', Arial, Verdana, sans-serif; font-size: 13px; line-height: 19.200000762939453px; margin-bottom: 1em; margin-top: 1em;">
<span style="font-size: x-small;">Running a <span class="IL_AD" id="IL_AD1" style="background-attachment: scroll !important; background-color: transparent !important; background-image: none !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: dotted !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; font-size: 13px !important; padding: 0px 0px 1px !important; position: static;">php code</span> in your local computer is very simple. You will need a </span>Web Server like Apache and PHP. If you want to run some database related code like login validation, and other database related tasks, you need a database server like MySQL.</div>
<div style="background-color: #fafafa; color: #152a3d; font-family: 'Lucida Grande', Arial, Verdana, sans-serif; font-size: 13px; line-height: 19.200000762939453px; margin-bottom: 1em; margin-top: 1em;">
You can install all of them individually, but a single product like <span class="IL_AD" id="IL_AD2" style="background-attachment: scroll !important; background-color: transparent !important; background-image: none !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: dotted !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; padding: 0px 0px 1px !important; position: static;">WAMP Server</span> will make everything easy. <strong>WAMP </strong>stands for <strong>Windows Apache MySQL and PHP</strong>. Installing and running WAMP Server is very easy when compared to others. And you don't need to configure hard stuff. Just follow the procedure.</div>
<div style="background-color: #fafafa; color: #152a3d; font-family: 'Lucida Grande', Arial, Verdana, sans-serif; font-size: 13px; line-height: 19.200000762939453px; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<h3 style="background-color: #fafafa; color: #152a3d; font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 1.4em; line-height: 19.200000762939453px; margin: 0.5em 0px;">
<span style="color: #ff6600;"><span style="font-size: small;">Installin WAMP Server to run PHP:</span></span></h3>
<ol style="background-color: #fafafa; color: #152a3d; font-family: 'Lucida Grande', Arial, Verdana, sans-serif; font-size: 13px; line-height: 19.200000762939453px;">
<li>Goto <a href="http://www.wampserver.com/" style="color: #006680; font-weight: bold; text-decoration: initial;" target="_blank">www.wampserver.com</a> and download <strong>WAMP Server</strong>.<br /> </li>
<li>Just install it like other softwares by just clicking next next...<br /> </li>
<li>Now go to START menu of windows and start wampserver.<br />Generally, the path is <strong>Start -> WapmServer -> start WampServer</strong><br /> </li>
<li>Open your web browser and type <a href="http://localhost/" style="color: #006680; font-weight: bold; text-decoration: initial;" target="_blank">http://localhost</a> (or <a href="http://127.0.0.1/" style="color: #006680; font-weight: bold; text-decoration: initial;" title="http://127.0.0.1">http://127.0.0.1</a> ) <br />If you see a default WampServer home page, you installation is success.<br /> </li>
<li>Now put your php code in <span style="font-family: Tahoma;"><strong>www</strong></span> folder in your wamp installation directory.<br />Usually this is <span style="font-family: Tahoma;"><strong>C:\wamp\www</strong></span><br /> </li>
<li>Now type <span style="color: blue;"><strong><span style="font-size: x-small;"><span style="font-family: Tahoma;">http://localhost/filename.php</span></span></strong></span> in your browser. (where <em>filename</em> is your php file name)<br /> <br />It will execute your php code.. Thats it...</li>
</ol>
</div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0tag:blogger.com,1999:blog-9017378041031770110.post-30109770346205920052013-05-22T11:20:00.001-07:002013-05-22T11:25:34.810-07:00Microsoft Access Database Connection in Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTzjJQYx1teoUYuE-thFT0jHK52RE-Qpg4VHBzoJibDC9dW6K4wgkZmXfqoLjaolQg53ls7irMMKboNXgpAwnqHjmHMLEY48BvKrsC8MjozwdsXAYgiR-wmxD7bkKEjrWX6dqtaOHgH1Fs/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTzjJQYx1teoUYuE-thFT0jHK52RE-Qpg4VHBzoJibDC9dW6K4wgkZmXfqoLjaolQg53ls7irMMKboNXgpAwnqHjmHMLEY48BvKrsC8MjozwdsXAYgiR-wmxD7bkKEjrWX6dqtaOHgH1Fs/s1600/Untitled.png" /></a></div>
<br /></div>
Ashish Lahotihttp://www.blogger.com/profile/12093716466166916972noreply@blogger.com0