list in j2me client stub - web-services

I create a web service that interact with database by hibernate.
the problem is:
when i want to create J2ME client stub, i receive some error(Unsupported tag in element jobs),
and i see my code and i found why that happen (it happen because in my Jobs entity i have a property type java.util.List but J2ME don't support list collection).
this is my Jobs entity:
public class Jobs implements java.io.Serializable{
private int id;
private String title;
private CompUsers compusers;
....
private Date publishDate;
private List requerment;
....
// geter and seter
}
my question is: how can i create J2ME client stub with this problem?

Related

GCP Pub/Sub: How to get event details in onFailure() of PublishCallbackListener

We want to have the fail back mechanism in case of any failure to publish event to Pub/Sub. I am using "ListenableFutureCallback" to know message published successfully or not. In case of failure, it is just throwing exception and I need event details to post it to internal messaging service. How do I get event details in onFailure() method.
I am using Spring Integration.
Below is piece of code.
Listener:
#Component
public class PubSubOperationListener implements ListenableFutureCallback<String> {
private static Logger LOGGER = LoggerFactory.getLogger(PubSubOperationListener.class);
#Override
public void onFailure(Throwable throwable) {
LOGGER.error("Failed to publish the message and details : {}",throwable);
// Logic to process it using different approach.
}
#Override
public void onSuccess(String s) {
LOGGER.info("Message published successfully.");
}
ServiceActivator:
PubSubMessageHandler pubSubMessageHandler = new PubSubMessageHandler(pubSubTemplate, testTopic);
pubSubMessageHandler.setPublishCallback(pubSubOperationListener);
return pubSubMessageHandler;
Please suggest if there is different approach to do same.
Currently, it's not possible because Spring Cloud GCP simply delegates to the Pub/Sub Publisher in the client library.
However, when we wrap the Future provided by the Publisher in Spring Cloud GCP, we can potentially include the original message there and other metadata. This would be a feature request that should be filed here.

Test a spring boot microservice that needs to call another microservice

I have followed this tutorial to create an e-commerce microservice architecure (in french) and now I am trying to write some tests. My architecture is composed of 4 microservices with Eureka and Zuul:
A product microservice that is here to provide a list of products
An order microservice that will handle the orders
A payment microservice that will handle the payments
A client UI
The payment microservice have to call the orders microservice to check if the order has already been payed or not. And this is what I can't reproduce to write unit tests. I would like to test this microservice without launching the orders microservice.
How can I test it without launching orders microservice?
I already wrote some tests for orders microservice and products microservice.
Here is the payment Controller:
/*
* Operations to save a payment and notify the orders microservice to update the status of the sayed oreder
**/
#PostMapping(value = "/payment")
public ResponseEntity<Payment> payAnOrder(#RequestBody Payment payment){
// We verify if the order has been already payed
System.out.println("We verify if the order has been already payed");
Payment existingPayment = paymentDao.findByidOrder(payment.getIdOrder());
if(existingPayment != null) throw new ExistingPaymentException("This order has already been payed!");
// We save the payment
System.out.println("We save the payment");
Payment newPayment = paymentDao.save(payment);
// if the DAO return null, there was a problem when saving the payment
System.out.println("if the DAO return null, there was a problem when saving the payment");
if(newPayment == null) throw new ImpossiblePaymentException("Error, impossible to establish the payment, retry later!");
// We retrieve the order corresponding to that payment by calling orders microservice
System.out.println("We retrieve the order corresponding to that payment by calling orders microservice");
Optional<OrderBean> orderReq = microserviceOrderProxy.retrieveOneOrder(payment.getIdOrder());
// orderReq.get() extract the object of type OrderBean from Optional
System.out.println("orderReq.get() extract the object of type OrderBean from Optional");
OrderBean order = orderReq.get();
// We update the object to mak the order as payed
System.out.println("We update the object to mak the order as payed");
order.setOrderPayed(true);
// We send the object updated to the orders microservice to update the order's status
System.out.println("We send the object updated to the orders microservice to update the order's status");
microserviceOrderProxy.updateOrder(order);
// We return 201 CREATED to notify the client that the payment has been registered
System.out.println("We return 201 CREATED to notify the client that the payment has been registered");
return new ResponseEntity<Payment>(newPayment, HttpStatus.CREATED);
}
I am blocked at the step where we retrieve the order corresponding to the payment because it tries to call orders microservice but its not running!
Here is the entire code: https://github.com/kamal951/POC_microservices
You can easily mock the other microservices you are calling in your unit test. In Mockito (which is bundled in spring-boot-starter-test), you can do this with the following approach:
public class PaymentControllerTest {
private PaymentController controller;
#Mock
private MicroserviceOrderProxy microserviceOrderProxy;
... other mocks here
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
controller = new PaymentController(microserviceOrderProxy, ...);
}
#Test
public void exampleTest() {
Mockito.when(microserviceOrderProxy.updateOrder(Mockito.any())).thenReturn(--mocked result here--);
...
}
}

I need to write a spring batch to call the web service is there any example

I have to write a batch (read/process/write) that call a web service (SOAP) as input and then process the result (list of items) to finally write them in a database. How can i call a web service
We did similar thing and here is our approach:
SOAP part:
We used WebServiceTemplate to communicate with SOAP server and method marshalSendAndReceive which basically sends xml request to some url and returns xml response
We use Spring Retry mechanism since SOAP communication is not always reliable so we did setup where we would do each SOAP call at least 5 times until we give up and fail job execution
We use Jaxb2Marshaller for serialization and deserialization and POJO generating from wsdl
Spring batch part:
We implement our own ItemReader where in #BeforeStep we fetch list of items to process from SOAP server (I am not sure if this is best approach but with retry mechanism in place this is robust enough), our #Override of read method is nothing special, it is walking over list until exhausted
In processor we translate SOAP item to DB entity
In writer we do save of items to our own DB
Example:
Item reader is using SoapClient which is my wrapper around web template and it is doing soap call, unmarshalling response and returning list of items.
#Component
#StepScope
public class CustomItemReader implements ItemReader<SoapItem> {
private List<SoapItem> soapItems;
#Autowired
private SoapClient soapClient;
#BeforeStep
public void beforeStep(final StepExecution stepExecution) throws Exception {
soapItems = soapClient.getItems();
}
#Override
public SoapItem read() {
if (!soapItems.isEmpty()) {
return soapItems.remove(0);
}
return null;
}
}

Truncated Java object when passing through JAX-WS WebService

I am currently working on a project that uses JAX-WS webservices in Java.
The global topic is this : the user creates locally an object, let's say an Agent. He calls a first webservice and passes its Agent to the webservice. The webservice treats the Agent (modifies its properties : e.g. lifepoints), and passes it to another webservice. This call is made from the first webservice, so the user has nothing to do in the process.
After a chain of several webservices, the user retrieves the Agent that has been modified.
The aim of my project is to design 2 parts:
a framework that specifies the behaviour previously described : webservices, Agents and the process of migration
a demo application using my framework. The main difference is the addition of a GUI and a new class Avatar, that extends Agent. So the migration process is still being done "by the framework", with Agent objects.
The following code shows a simple example of how I call my webservice, host my Avatar, then retrieves the agent from the service :
// connection to the server
URL endpoint= new URL("http://SERVER/tomcat/KiwiBidonDynamique/ServiceWebBidonDeDadou?wsdl");
QName serviceName=new QName("http://avatar/","ServeurKiwiBidonService");
Service service = Service.create(endpoint, serviceName);
WebService port = service.getPort(WebService.class);
Avatar myAvatar = new Avatar(1, "Jack the Ripper");
port.hostAgent(myAvatar);
// some process on the service...
Avatar myAvatarTransformed = (Avatar) port.getAgent("AgentNumberOne");
When I do that, I get an exception on the final line :
Exception in thread "main" java.lang.ClassCastException: agent.Agent cannot be cast to avatar.Avatar
After a lot of log reading, I guess the reason is the way the webservice works. When being called, my Avatar given in parameter is marshalled in my JVM then unmarshalled on the service, but the service only constructs an Agent when it unmarshalles. Doing so, it truncates the data specific to the Avatar. Then when I try to retrieve my Agent from the service, it cannot be cast to an Avatar.
Is there a way to keep the Avatar information while processing as an Agent on the service ?
Can I write my own marshalling/unmarshalling somehow ?
Thanks a lot.
If your webservice has Agent element defined as incoming data, then no it is not possible to unmarshall it into an inherited class. I guess it would be possible to write your own marshaller but it is not as easy as it sounds (I would advise against it). Either write a separate WS for each class (messy) or make the incoming data have an element that can store additional structures, like type:any (also messy). The truth is WS are not exactly OO.

Spring MessageListener multiple Messages

I am implementing a Spring MessageListener that is listening to a JMS Queue to process messages containing XML.
My bean ProposalSOAListener will be processing about 5 or more XML messages from the queue. My code is below.
Is there a way to specify different methods on this class to handle different XML messages?
public class ProposalSOAListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println(((TextMessage) message).getText());
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
} // end of ProposalSOAListener class
There's a bunch of architectural questions begged by your question. Do you want this mesasge listener to do the work, or hand it off to another component? Are there transactional considerations at play? Do you have memory constraints - i.e. do you want streaming based XML processing or not? Do
The good news is that you have a lot of the pieces to this puzzle available to you within Spring.
A simple next step would be to use Spring Object XML Marshalling (OXM), choose one of the techniques, and wire the marshaller into your listener bean.
See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/oxm.html
Another technique would be to use the Spring ApplicationEvent interface, read the messages coming in off the queue and publish them internally to listeners of the specific types. That could be used in combination with the above object marshalling.
Last but not least, if this is SOAP web services - you can take a look at Spring WS, it uses the similar message containers to pull messages off the wire, marshall them, and invoke a spring ws endpoint (ie. the service interface that satisfies that interface contract).
http://static.springsource.org/spring-ws/sites/2.0/reference/html/server.html#d4e907
Spring Integration project is highly recommended for this kind of a problem. Essentially you will have to implement a jms inbound gateway to get your message in. You can then transform this to an object at this point, then route the message to the appropriate service-activator component, which can map to your instance and method.