Currently i am using JpaItemWriter to write the list of objects as below which is working fine.
Now i want to change the JpaItemWriter to JdbcBatchItemWriter due to performance issue.
public class MyItemWriter implements ItemWriter<List<MyDomainObject>> {
#Override
public void write(List<? extends Lists<MyDomainObject>> items) {
JpaItemWriter<MyDomainObject> writer = new JpaItemWriter<>();
for(List<MyDomainObject> o : items)
{
writer.write(o);
}
}
}
Suggest a sample snippets which uses the JdbcBatchItemWriter to write the List of objects will helps. Tried using the ItemSqlParameterSourceProvider it did't help ending up in org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter exception
You example is not correct. You are creating a JpaItemWriter in the write method, so a new instance is created on each call to write. This is probably the cause of your performance issue.
More importantly, lifecycle methods of the delegate writer (open/update/close) will not be honored (it is not the case for JpaItemWriter which does not implement ItemStream but this would be a problem if the delegate is an item stream). Your MyItemWriter implementation should be something like:
public class MyItemWriter implements ItemWriter<List<MyDomainObject>> {
private JpaItemWriter jpaItemWriter;
public MyItemWriter(JpaItemWriter jpaItemWriter) {
this. jpaItemWriter = jpaItemWriter;
}
#Override
public void write(List<? extends Lists<MyDomainObject>> items) {
for(List<MyDomainObject> o : items) {
this. jpaItemWriter.write(o);
}
}
}
Now if you want to use the JdbcBatchItemWriter to write a list of lists, see Spring Batch - Using an ItemWriter with List of Lists.
Edit: Added a sample code of how to set the delegate as requested in comments:
#Bean
public ListUnpackingItemWriter<T> itemWriter() {
JdbcBatchItemWriter<T> jdbcBatchItemWriter = null; // configure your jdbcBatchItemWriter
ListUnpackingItemWriter<T> listUnpackingItemWriter = new ListUnpackingItemWriter<>();
listUnpackingItemWriter.setDelegate(jdbcBatchItemWriter);
return listUnpackingItemWriter;
}
Related
I've created a Xamarin.Forms UWP app and it felt to me like a bucket full of cement on your feet while trying to cross the mediterranean by swimming. So I switched to native UWP programming.
My application gets created to 95 % dynamically. All pages controls on the pages are dependend on what's saved in the database.
I have one login-page, a carouselPage (FlipView) and a QuitPage.
All other pages are dynamically loaded and added to the FlipView-ItemsSource.
In Xamarin I had a TemplateSelector like the one in this link:
https://github.com/alexrainman/CarouselView/blob/master/Demo/Views/MyTemplateSelector.cs
My question is:
How can I dynamically create DateTemplates out of my Views/Pages?
BTW:
I switched from Xamarin to native because there were heavy performance issues. The Carousel switches everytime to the first item and needed several seconds to show the "actual" page again, after the size was changed (through virtual keyboard).
I hope for a solution without such performance issues when there are more than 30 items in the list.
Maybe someone can explain why there is such a performance issue? I can't believe that every page is gonna be reloaded caused by the size changed by the virtual kb.
Thank you all in advance.
EDIT:
After I found the button for editing, here's what I got so far:
I have a Page named CarouselView. In the Xaml I have the FlipView named "myCarousel".
In the CodeBehind I have the following code:
public partial class WellcomeView : BasePage {
#region Fields + Properties
private int _position;
public int Position { get { return _position; } set { _position = value; } }
#endregion
public CarouselView() {
InitializeComponent();
myCarousel.ItemTemplateSelector = new MyTemplateSelector();
DataContext = new WellcomeViewModel(null, null, null, null, null);
}
public class MyTemplateSelector : Windows.UI.Xaml.Controls.DataTemplateSelector {
private DataTemplate GreetingTemplate;
...
public MyTemplateSelector() {
this.GreetingTemplate = (DataTemplate)Windows.UI.Xaml.Markup.XamlReader.Load(new GreetingView().Content.ToString());
...
// Here it throws an error. No matter if MainPage (with content) or other Views (with Content==null). But in every case I get a XamlParseException.
} // End Ctor
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) {
var TemplateType = item.GetType();
switch (TemplateType.Name) {
case "GreetingViewModel":
return base.SelectTemplateCore(GreetingTemplate );
default:
return base.SelectTemplateCore(BegruessungTemplate);
} // End switch-case
} // End SelectTemplateCore()
} // End MyTemplateSelector()
} // End class WellcomeView
Here's the Error I get...
And here's the StackTrace:
at Windows.UI.Xaml.Markup.XamlReader.Load(String xaml)
at MEDePORT_Win_Native.Views.WellcomeView.MyTemplateSelector..ctor()
at MEDePORT_Win_Native.Views.WellcomeView..ctor()
at MEDePORT_Win_Native.MEDePORT_Win_Native_XamlTypeInfo.XamlTypeInfoProvider.Activate_11_WellcomeView()
at MEDePORT_Win_Native.MEDePORT_Win_Native_XamlTypeInfo.XamlUserType.ActivateInstance()
at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter)
at MEDePORT_Win_Native.App.OnLaunched(LaunchActivatedEventArgs e)
EDIT:
With "new MyView().Content.ToString()" I only get the name of the type. For example "Windows.Ui.Xaml.Controls.Grid".
So it seems like I need to extract xaml out of a view and convert it to a string.
EDIT EDIT EDIT EDIT EDIT:
What is the UWP equivalent for the following code?
DataTemplate template = new DataTemplate { DataType = typeof(< Type of the object the template refers>) };
Try to create your templates not as xaml-Files. Just copy the the Template-content to a xaml-DataTemplate in a new Resources.Xaml.
Then create Your TemplateSelector like this:
public class MyTemplateSelector : DataTemplateSelector {
public DataTemplate GreetingTemplate;
static ResourceDictionary dataTemplates;
public MyTemplateSelector() {
if (dataTemplates == null) {
dataTemplates = new Templates.DataTemplates();
}
GreetingTemplate = dataTemplates["GreetingTemplate"] as DataTemplate; // Look how to load Your Templates!
}
And return your templates like this:
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) {
var TemplateType = item.GetType();
switch (TemplateType.Name) {
case "GreetingViewModel":
return GreetingTemplate;
default:
return base.SelectTemplateCore(GreetingTemplate);
}
}
The Class Templates.DataTemplates() from the first code example looks like this:
public partial class DataTemplates : ResourceDictionary {
public DataTemplates() {
InitializeComponent();
}
}
and it's xaml contains your DataTemplates.
Hope, this helps.
I am working on a very simple program, looking like this:
public class WicketApplication extends WebApplication implements Comparable<Object>{
private List<Person> persons = Arrays.asList(
new Person("Mikkel", "20-02-91", 60169803),
new Person("Jonas", "02-04-90", 86946512),
new Person("Steffen", "15-07-90", 12684358),
new Person("Rasmus", "08-12-93", 13842652),
new Person("Michael", "10-10-65", 97642851));
/**
* #see org.apache.wicket.Application#getHomePage()
*/
#Override
public Class<? extends WebPage> getHomePage() {
return SimpleView.class;
}
public static WicketApplication get() {
return (WicketApplication) Application.get();
}
/**
* #return #see org.apache.wicket.Application#init()
*/
public List<Person> getPersons() {
return persons;
}
public List<Person> getSortedList(){
return Collections.sort(persons);
//This won't work before implementing comparator i know, but how??
}
#Override
public void init() {
super.init();
// add your configuration here
}
#Override
public int compareTo(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
That was the class where i just put my people into a list.
public class SimpleView extends SimpleViewPage {
public SimpleView() {
ListView persons = new ListView("persons", getPersons()) {
#Override
protected void populateItem(ListItem item) {
Person person = (Person) item.getModelObject();
item.add(new Label("name", person.getName()));
item.add(new Label("birthdate", person.getBirthdate()));
item.add(new Label("phone", person.getPhone()));
}
};
add(persons);
add(new Label("size", "Number of people " + getPersons().size()));
}
}
And here is what i do with the people.
Basicly i want the program to show a table with all the data(this already works).
Now i want to be able to sort them. But i can't for the life of me figure it out. I'm still rather new at programming, and i want to have a button below my table that can sort on name, bday or phone number. Was thinking about trying to Comparable, but can't remember it that well, and not sure how it works with Wicket..
Thanks for the help in advance :)
What you need is the DataView component, which provides all the support you need for sorting (and paging, should you require it later on).
Here's a working example, if you click on the "Source Code" link in the top right corner, you can see that most of the things you want from a sortable table work out of the box. All you need is to create a suitable data provider.
If you use DataView with a SortableDataProvider, you don't need to worry about writing your own dynamic Comparator. (Which is not a terribly hard task itself, but it's easy to get it wrong.)
I have the following four classes: DataConsumer, DataProducer, SomeQualifier, a META-INF/beans.xml and a test. The class files are coded as follows:
public class DataConsumer {
private boolean loaded = false;
#Inject
#SomeQualifier
private String someString;
public void afterBeanDiscovery(
#Observes final AfterBeanDiscovery afterBeanDiscovery,
final BeanManager manager) {
loaded = true;
}
public boolean getLoaded() {
return loaded;
}
public String sayHello() {
return someString;
}
}
public class DataProducer {
#Produces
#SomeQualifier
private final String sample = "sample";
}
public #interface SomeQualifier {
}
The unit test looks like this.
public class WeldTest {
#Test
public void testHelloWorld() {
final WeldContainer weld = new Weld().initialize();
final DataConsumer consumer = weld.instance()
.select(DataConsumer.class).get();
Assert.assertEquals("sample", consumer.sayHello());
Assert.assertTrue(consumer.getLoaded());
}
}
However, it is failing on the assertTrue with getLoaded() it appears that the #Observes does not get fired.
Take a look at arquillian: www.arquillian.org. It'll take care of all of this for you.
I found a similar question that had answered my question
CDI - Observing Container Events
Although I am unable to use DataConsumer as both an Extension and a CDI managed bean. So it needs a third class just to be the Extension. However, because Extension have no access to managed beans since they are not created yet, I conclude that is no possible solution to use an #Observes AfterBeanDiscovery to modify the bean data. Even the BeanManager that gets passed in cannot find any of the beans.
In wicket without saving to the session how can i have a persistent object for example a list which can be set in one panel and accessed from another. Iv done a lot of googleing and im not entirely sure how this would work. Any help would be appreciated greatly! Thank you.
Related to the comments above, I will try and explain what I was thinking.
Disclaimer: It's been more than a year since I worked with Wicket, so the following should be read as an overall proof-of-concept. I cannot guarantee that it will compile (actually, I can almost certainly guarantee that it will not.)
public class MyPage extends ... {
...
MyPageModel pm = new MyPageModel();
add(new MyPanel1(pm));
add(new MyPanel2(pm));
...
}
public class MyPageModel implements Serializable {
private IModel<List<MyDataObject>> dataObjects;
public MyPageModel() {
this.dataObjects = // Load list from somewhere
}
public IModel<List<MyDataObject>> getDataObjects() {
return this.dataObjects;
}
}
public class MyPanel1 extends ... {
private MyPageModel pageModel;
public MyPanel1(MyPageModel pageModel) {
this.pageModel = pageModel;
...
add(new ListSomethingComponent<MyDataObject>(pageModel.getDataObjects)); // Some list renderer component which takes a IModel<List<MyDataObject>> as data
}
}
public class MyPanel2 extends ... {
private MyPageModel pageModel;
public MyPanel2(MyPageModel pageModel) {
// Same as MyPanel1...
}
}
I have an adapter class for Linq-to-Sql:
public interface IAdapter : IDisposable
{
Table<Data.User> Activities { get; }
}
Data.User is an object defined by Linq-to-Sql pointing to the User table in persistence.
The implementation for this is as follows:
public class Adapter : IAdapter
{
private readonly SecretDataContext _context = new SecretDataContext();
public void Dispose()
{
_context.Dispose();
}
public Table<Data.User> Users
{
get { return _context.Users; }
}
}
This makes mocking the persistence layer easy in unit testing, as I can just return whatever collection of data I want for Users (Rhino.Mocks):
Expect.Call(_adapter.Users).Return(users);
The problem is that I cannot create the object 'users' since the constructors are not accessible and the class Table is sealed. One option I tried is to just make IAdapter return IEnumerable or IQueryable, but the problem there is that I then do not have access to the methods ITable provides (e.g. InsertOnSubmit()). Is there a way I can create the fake Table in the unit test scenario so that I may be a happy TDD developer?
My current solution is to wrap the functionality I want from Table into a TableWrapper class:
public interface ITableWrapper<TEntity>
where TEntity : class
{
IEnumerable<TEntity> Collection { get; }
void InsertOnSubmit(TEntity entity);
}
And here's the implementation:
public class TableWrapper<TEntity> : ITableWrapper<TEntity>
where TEntity : class
{
private readonly Table<TEntity> _table;
public TableWrapper(Table<TEntity> table)
{
_table = table;
}
public IEnumerable<TEntity> Collection
{
get { return _table; }
}
public void InsertOnSubmit(TEntity entity)
{
_table.InsertOnSubmit(entity);
}
}
So now I can easily mock data from Collection, as well as keeping the functionality of InsertOnSubmit (any other functions that I need down the road can be added later on).
I have had success using the Data Access Layer to produce domain object collections and then using linq to objects.
The object under test then only relates to List, which is fairly easy to unit test.
I don't like when the logic entities should have Data Access Layer dependencies. They should stop at the service layer, if even there. I usually go for the model where the service layer invokes a data access object to get a List, passes that list into whichever logic object that needs it (if necessary uses linq-to-objects to filter out the relevant data and injects it into eiter a flat list, dictionary or an object model).
The business objects become very testable, even though they don't benefit from the richness of the inferred data model.