/**
* Checks if the value is a valid credit card number
* @param value a string
* @return true if the value is a valid credit card number
*/
public static boolean isValid(String value){
return luhnCheck(value.replaceAll("\\D", "")); //remove non-digits
}
/**
* Checks if a cardNumber passes LUHN check
* @param cardNumber A string contains only digits
* @return true if the cardNumber passes LUHN check
*/
private static boolean luhnCheck(String cardNumber){
int sum=0;
for (int i=cardNumber.length()-1; i>=0; i-=2){
sum+=Integer.parseInt(cardNumber.substring(i, i+1));
if (i>0){
int d=2*Integer.parseInt(cardNumber.substring(i-1, i));
if (d>9) d-=9;
sum+=d;
}
}
return sum%10==0;
}
Thursday, 17 November 2011
Credit Card Validation --- Luhn Check
Configure JMS Resources and create Message Driven Bean in Websphere 7 (Step 8 and 9)
Step 8: Publish a Topic JMS message
Similar to step 4, omit the steps for context lookup, use dependency injection only.
8.1 Access the JMS resources in EJB
In ibm-ejb-jar-bnd.xml under META-INF
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Create a stateless session bean
Note: the name attribute of @Resource annotation should match name attribute of <resource-ref> and <message-destination-ref> element respectively in ibm-ejb-jar-bnd.xml, not the binding-name attribute.
Write a servlet to test
Result
[26/08/11 15:18:00:625 EST] 0000008f SystemOut O Broadcasted
8.2 Access the JMS resources in servlet
In ibm-web-bnd.xml under WEB-INF
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Write a servlet to test
Result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
Step 9: Create Topic Message Driven Beans
9.1 Create a Topic Activation Specification
Similar to 5.1
Type TopicActSpec for Name
jms/TopicActSpec for JNDI name
Select Topic as Destination type
Type jms/Topic for Destination JNDI name
Select InternalJMS as Bus name
Click ‘OK’ and save the changes.
9.2 Create two Topic Message Driven Beans
In ibm-ejb-jar-bnd.xml under META-INF
Test the servlet created in step 8 and check the result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
[26/08/11 15:23:36:218 EST] 000000a3 SystemOut O This is a broadcast processed by bean 2
[26/08/11 15:23:36:218 EST] 000000a6 SystemOut O This is a broadcast processed by bean 1
Similar to step 4, omit the steps for context lookup, use dependency injection only.
8.1 Access the JMS resources in EJB
In ibm-ejb-jar-bnd.xml under META-INF
<session name="jmsService"> <resource-ref name="jms/MyTopicConnectionFactory" binding-name="jms/TopicConnectionFactory"> </resource-ref> <message-destination-ref name="jms/MyTopic" binding-name="jms/Topic"/> </session>
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Create a stateless session bean
@Stateless(name = "jmsService")
@Local(JMSService.class)
public class JMSServiceBean implements JMSService {
@Resource(name = "jms/MyTopicConnectionFactory")
private TopicConnectionFactory tcf;
@Resource(name = "jms/MyTopic")
private Topic topic;
@Override
public void broadcast() {
try {
TopicConnection connection = tcf.createTopicConnection();
TopicSession session
= connection.createTopicSession
(false, TopicSession.AUTO_ACKNOWLEDGE);
TopicPublisher publisher
= session.createPublisher(topic);
TextMessage message = session.createTextMessage();
message.setText("This is a broadcast");
publisher.send(message);
} catch (JMSException e) {
throw new EJBException(e);
}
System.out.println("Broadcasted");
}
}Note: the name attribute of @Resource annotation should match name attribute of <resource-ref> and <message-destination-ref> element respectively in ibm-ejb-jar-bnd.xml, not the binding-name attribute.
Write a servlet to test
public class JMSTopicPublishServlet extends HttpServlet {
@EJB(beanName="jmsService")
private JMSService jmsService;
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
jmsService.broadcast();
}
}
Result
[26/08/11 15:18:00:625 EST] 0000008f SystemOut O Broadcasted
8.2 Access the JMS resources in servlet
In ibm-web-bnd.xml under WEB-INF
<resource-ref name="jms/MyTopicConnectionFactory" binding-name="jms/TopicConnectionFactory"> </resource-ref> <message-destination-ref name="jms/MyTopic" binding-name="jms/Topic"/>
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Write a servlet to test
public class JMSTopicPublishServlet extends HttpServlet {
@Resource(name = "jms/MyTopicConnectionFactory")
private TopicConnectionFactory tcf;
@Resource(name = "jms/MyTopic")
private Topic topic;
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
try {
TopicConnection connection = tcf.createTopicConnection();
TopicSession session
= connection.createTopicSession
(false, TopicSession.AUTO_ACKNOWLEDGE);
TopicPublisher publisher
= session.createPublisher(topic);
TextMessage message = session.createTextMessage();
message.setText("This is a broadcast");
publisher.send(message);
} catch (JMSException e) {
throw new EJBException(e);
}
System.out.println("Broadcasted");
}
}
Result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
Step 9: Create Topic Message Driven Beans
9.1 Create a Topic Activation Specification
Similar to 5.1
Type TopicActSpec for Name
jms/TopicActSpec for JNDI name
Select Topic as Destination type
Type jms/Topic for Destination JNDI name
Select InternalJMS as Bus name
Click ‘OK’ and save the changes.
9.2 Create two Topic Message Driven Beans
@MessageDriven(name="topicMessageDrivenBean1")
public class TopicMessageDrivenBean1 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage txtMessage = (TextMessage) message;
System.out.println(txtMessage.getText()
+ " processed by bean 1");
} catch (JMSException ex) {
throw new EJBException(ex);
}
}
}
@MessageDriven(name="topicMessageDrivenBean2")
public class TopicMessageDrivenBean2 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage txtMessage = (TextMessage) message;
System.out.println(txtMessage.getText()
+ " processed by bean 2");
} catch (JMSException ex) {
throw new EJBException(ex);
}
}
}
In ibm-ejb-jar-bnd.xml under META-INF
<message-driven name="topicMessageDrivenBean1"> <jca-adapter activation-spec-binding-name="jms/TopicActSpec"/> </message-driven> <message-driven name="topicMessageDrivenBean2"> <jca-adapter activation-spec-binding-name="jms/TopicActSpec"/> </message-driven>
Test the servlet created in step 8 and check the result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
[26/08/11 15:23:36:218 EST] 000000a3 SystemOut O This is a broadcast processed by bean 2
[26/08/11 15:23:36:218 EST] 000000a6 SystemOut O This is a broadcast processed by bean 1
Subscribe to:
Comments (Atom)
